在Ember js中,如何在单元测试中创建或模拟hasMany关系
In Ember js, how to create or mock hasMany relationship in unit test
我正在对具有 DS.hasMany()
关系属性的模型进行单元测试。每当我进行以下单元测试时,我的测试运行器中都会不断出现此错误:
Error: Assertion Failed: All elements of a hasMany relationship must be instances of DS.Model, you passed [<Ember.Object:ember367>,<Ember.Object:ember368>]
有人可以解释一下吗?
型号:
export default DS.Model.extend({
accounts: DS.hasMany('account'),
servicesAccounts: DS.hasMany('services-account'),
address: MF.fragment('address'),
appEligibilities: MF.fragmentArray('app-eligibility'),
appsForPremise: Ember.computed('accounts', function () {
return DS.PromiseArray.create({
promise: this.get('store').find('app', {
account: this.get('accounts').mapBy('id')
})
});
})
});
模型单测:
import { moduleForModel, test } from 'ember-qunit';
import Ember from 'ember';
moduleForModel('premise', 'Unit | Model | premise', {
needs: [
'model:account',
'model:services-account',
'model:address',
'model:app-eligibility'
]
});
test('Apps for premise', function (assert) {
let model = this.subject({
accounts: [Ember.Object.create({
id: 'account-1'
}),
Ember.Object.create({
id: 'account-2'
})],
appsForPremise: sinon.spy()
});
Ember.run(() => {
});
assert.equal(model.get('appsForPremise'), '[{id: account-1}, {id: account-2}]');
});
您不能将常规 ember 对象传递给 hasMany 关系,它们必须是存储模型对象。您可以使用商店创建它们,即
const store = this.store();
Ember.run(() => {
const model = this.subject({
accounts: [
store.createRecord('services-account', {
id: 'account-1'
}),
store.createRecord('services-account',{
id: 'account-2'
})],
appsForPremise: sinon.spy()
});
});
调用存储方法必须进入 运行 循环,否则 Ember 会报错。
我正在对具有 DS.hasMany()
关系属性的模型进行单元测试。每当我进行以下单元测试时,我的测试运行器中都会不断出现此错误:
Error: Assertion Failed: All elements of a hasMany relationship must be instances of DS.Model, you passed [<Ember.Object:ember367>,<Ember.Object:ember368>]
有人可以解释一下吗?
型号:
export default DS.Model.extend({
accounts: DS.hasMany('account'),
servicesAccounts: DS.hasMany('services-account'),
address: MF.fragment('address'),
appEligibilities: MF.fragmentArray('app-eligibility'),
appsForPremise: Ember.computed('accounts', function () {
return DS.PromiseArray.create({
promise: this.get('store').find('app', {
account: this.get('accounts').mapBy('id')
})
});
})
});
模型单测:
import { moduleForModel, test } from 'ember-qunit';
import Ember from 'ember';
moduleForModel('premise', 'Unit | Model | premise', {
needs: [
'model:account',
'model:services-account',
'model:address',
'model:app-eligibility'
]
});
test('Apps for premise', function (assert) {
let model = this.subject({
accounts: [Ember.Object.create({
id: 'account-1'
}),
Ember.Object.create({
id: 'account-2'
})],
appsForPremise: sinon.spy()
});
Ember.run(() => {
});
assert.equal(model.get('appsForPremise'), '[{id: account-1}, {id: account-2}]');
});
您不能将常规 ember 对象传递给 hasMany 关系,它们必须是存储模型对象。您可以使用商店创建它们,即
const store = this.store();
Ember.run(() => {
const model = this.subject({
accounts: [
store.createRecord('services-account', {
id: 'account-1'
}),
store.createRecord('services-account',{
id: 'account-2'
})],
appsForPremise: sinon.spy()
});
});
调用存储方法必须进入 运行 循环,否则 Ember 会报错。