我有一个使用this.get('model.property')的组件,它按预期工作。
对于我的集成测试,我使用的是幻影,它适用于我的所有其他测试(包括集成测试),但是,当我测试这个特定的组件时,我会得到:
TypeError: Cannot read property 'then' of undefined
我的测试是这样的:
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有这样的东西:
this.get('model.' + belongs).then(relationship => {...})其中,belongs只是调用组件时设置的任何belongs-to的值。
我有点困惑,因为我要通过测试的模拟模型似乎并不像运行ember s时那样表示模型(我也在为开发目的使用幻影)。有什么地方可以让我更多地了解那里到底发生了什么?
谢谢!
P.S.我还尝试使用由location提供的server.create()对象,并得到了一个稍微不同的错误:
TypeError: _this.get(...).then is not a function
发布于 2017-08-08 22:10:39
通过阅读this answer,我找到了自己的解决方案,这个解决方案非常有效:
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}}
`)基本上,我选择了直接使用商店,而不是使用幻影,它的工作!
https://stackoverflow.com/questions/45576493
复制相似问题