gpt4 book ai didi

javascript - 在 Ember js 中,如何在单元测试中创建或模拟 hasMany 关系

转载 作者:行者123 更新时间:2023-12-04 00:29:02 25 4
gpt4 key购买 nike

我正在对具有 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()
});
});

对 store 方法的调用必须进入运行循环,否则 Ember 会报错。

关于javascript - 在 Ember js 中,如何在单元测试中创建或模拟 hasMany 关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38517927/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com