admin管理员组文章数量:1390626
I'm unit-testing a model which has properties with DS.hasMany()
relationships. Whenever I do the following unit-test, I keep getting this error in my test-runner:
Error: Assertion Failed: All elements of a hasMany relationship must be instances of DS.Model, you passed [<Ember.Object:ember367>,<Ember.Object:ember368>]
Can someone shed some light into this, please?
Model:
export default DS.Model.extend({
accounts: DS.hasMany('account'),
servicesAccounts: DS.hasMany('services-account'),
address: MF.fragment('address'),
appEligibilities: MF.fragmentArray('app-eligibility'),
appsForPremise: Emberputed('accounts', function () {
return DS.PromiseArray.create({
promise: this.get('store').find('app', {
account: this.get('accounts').mapBy('id')
})
});
})
});
Model uni-test:
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}]');
});
I'm unit-testing a model which has properties with DS.hasMany()
relationships. Whenever I do the following unit-test, I keep getting this error in my test-runner:
Error: Assertion Failed: All elements of a hasMany relationship must be instances of DS.Model, you passed [<Ember.Object:ember367>,<Ember.Object:ember368>]
Can someone shed some light into this, please?
Model:
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.puted('accounts', function () {
return DS.PromiseArray.create({
promise: this.get('store').find('app', {
account: this.get('accounts').mapBy('id')
})
});
})
});
Model uni-test:
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}]');
});
Share
Improve this question
edited Jul 22, 2016 at 11:18
Shaoz
asked Jul 22, 2016 at 3:59
ShaozShaoz
10.7k26 gold badges76 silver badges100 bronze badges
1 Answer
Reset to default 11You cant pass regular ember objects to the hasMany relationship, they have to be store model objects. You can create them using the store, i.e.
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()
});
});
Calls to the store methods have to go into the run loop, otherwise Ember will plain.
本文标签: javascriptIn Ember jshow to create or mock hasMany relationship in unit testStack Overflow
版权声明:本文标题:javascript - In Ember js, how to create or mock hasMany relationship in unit test - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744628210a2616385.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论