首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Ember.js +幻影:在集成测试中提取一个模拟的关系

Ember.js +幻影:在集成测试中提取一个模拟的关系
EN

Stack Overflow用户
提问于 2017-08-08 19:21:54
回答 1查看 554关注 0票数 1

我有一个使用this.get('model.property')的组件,它按预期工作。

对于我的集成测试,我使用的是幻影,它适用于我的所有其他测试(包括集成测试),但是,当我测试这个特定的组件时,我会得到:

TypeError: Cannot read property 'then' of undefined

我的测试是这样的:

代码语言:javascript
复制
import { moduleForComponent, test } from 'ember-qunit'
import hbs from 'htmlbars-inline-precompile'
import { startMirage } from 'app/initializers/ember-cli-mirage'
import Ember from 'ember'

moduleForComponent('summary-card', 'Integration | Component | summary card', {
  integration: true,
  beforeEach() {
    this.server = startMirage()
  },
  afterEach() {
    this.server.shutdown()
  }
})

test('it renders', function(assert) {
  const customer = this.server.create('customer')
  const location = this.server.create('location', { customer })
  const manufacturer = this.server.create('manufacturer')
  const model = this.server.create('device-model', { manufacturer })
  this.server.createList('device', 5, { model, customer, location })

  const loc = Ember.Object.create(location)
  this.set('model', loc)
  this.render(hbs`{{summary-card model=model model-name=model.name tag='location' belongs-to='customer' has-many='device'}}`);

  assert.equal(this.$('h1').text().trim(), 'Location 0', 'should be titled Location 0')

});

基本上,我的summary-card.js有这样的东西:

代码语言:javascript
复制
this.get('model.' + belongs).then(relationship => {...})

其中,belongs只是调用组件时设置的任何belongs-to的值。

我有点困惑,因为我要通过测试的模拟模型似乎并不像运行ember s时那样表示模型(我也在为开发目的使用幻影)。有什么地方可以让我更多地了解那里到底发生了什么?

谢谢!

P.S.我还尝试使用由location提供的server.create()对象,并得到了一个稍微不同的错误:

TypeError: _this.get(...).then is not a function

EN

回答 1

Stack Overflow用户

发布于 2017-08-08 22:10:39

通过阅读this answer,我找到了自己的解决方案,这个解决方案非常有效:

代码语言:javascript
复制
import { moduleForComponent, test } from 'ember-qunit'
import hbs from 'htmlbars-inline-precompile'
import Ember from 'ember'

moduleForComponent('summary-card', 'Integration | Component | summary card', {
  integration: true
})

test('it renders', function(assert) {
  this.inject.service('store', {as: 'store'})
  let location

  Ember.run(() => {
    location = this.store.createRecord('location', {
      id: 0,
      name: 'Location 0',
      customer: this.store.createRecord('customer', {
        id: 1,
        name: 'Customer 0'
      }),
      devices: [this.store.createRecord('device', {id: 1})]
    })
  })

  this.set('model', location)
  this.render(hbs`
    {{#summary-card model=model model-name=model.name tag='location' belongs-to='customer' has-many='device'}}
      <div class='test-content'>Test Content</div>
    {{/summary-card}}
  `)

基本上,我选择了直接使用商店,而不是使用幻影,它的工作!

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45576493

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档