我正在对具有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}]');
});
您不能将常规余烬对象传递给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()
});
});
调用store方法必须进入运行循环,否则Ember会抱怨。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句