如何为 EmberJS 集成测试修复 'service injection'?
How to fix 'service injection' for EmberJS integration test?
我有一个使用 'current-user' 服务的 tasks-table 组件。我将服务注入到组件中,如下所示
import { inject as service } from '@ember/service';
export default Component.extend({
currentUser: service(),
showBatchAction: computed('currentUser.user.directRoles.@each.title', function() {
return this.get('currentUser.user.directRoles').toArray().some((role) => {
return (role.id == 13) || (role.id == 15)
});
}),
});
代码工作正常,但是集成测试失败。
测试:
module('Integration | Component | tasks-table export csv button', function(hooks) {
setupRenderingTest(hooks);
hooks.beforeEach(function() {
run(() => {
this.owner.unregister('service:current-user');
});
this.owner.register('serivce:current-user', Service.extend({
user: EmberObject.create({
first_name: 'Bob',
last_name: 'Newby',
role: 'client',
directRoles: {title: 'employee', id: 1}
})
}));
});
test('it renders', async function(assert) {
var component = this.subject({
authManager: stubMyService.create()
});
await render(hbs `{{tasks-table}}`);
assert.equal(this.$('.export-csv').text().trim(), 'Export as CSV');
});
});
我得到的错误是:TypeError: undefined is not an object (evaluating 'this.get('currentUser.user.directRoles').toArray')
directRoles 不是数组?你怎么在一个对象上循环(某种方法)。通过说 object.toArray() 不起作用。
我有一个使用 'current-user' 服务的 tasks-table 组件。我将服务注入到组件中,如下所示
import { inject as service } from '@ember/service';
export default Component.extend({
currentUser: service(),
showBatchAction: computed('currentUser.user.directRoles.@each.title', function() {
return this.get('currentUser.user.directRoles').toArray().some((role) => {
return (role.id == 13) || (role.id == 15)
});
}),
});
代码工作正常,但是集成测试失败。
测试:
module('Integration | Component | tasks-table export csv button', function(hooks) {
setupRenderingTest(hooks);
hooks.beforeEach(function() {
run(() => {
this.owner.unregister('service:current-user');
});
this.owner.register('serivce:current-user', Service.extend({
user: EmberObject.create({
first_name: 'Bob',
last_name: 'Newby',
role: 'client',
directRoles: {title: 'employee', id: 1}
})
}));
});
test('it renders', async function(assert) {
var component = this.subject({
authManager: stubMyService.create()
});
await render(hbs `{{tasks-table}}`);
assert.equal(this.$('.export-csv').text().trim(), 'Export as CSV');
});
});
我得到的错误是:TypeError: undefined is not an object (evaluating 'this.get('currentUser.user.directRoles').toArray')
directRoles 不是数组?你怎么在一个对象上循环(某种方法)。通过说 object.toArray() 不起作用。