我正在奥雷利亚项目中测试我的一个组件。我想在我的单元测试中访问我的组件的viewModel,但是到目前为止还没有任何进展。
我遵循了https://aurelia.io/docs/testing/components#manually-handling-lifecycle中可用的示例,但我一直得到component.viewModel
is undefined
。
下面是单元测试:
describe.only('some basic tests', function() {
let component, user;
before(() => {
user = new User({ id: 100, first_name: "Bob", last_name: "Schmoe", email: 'joe@schmoe.com'});
user.save();
});
beforeEach( () => {
component = StageComponent
.withResources('modules/users/user')
.inView('<user></user>')
.boundTo( user );
});
it('check for ', () => {
return component.create(bootstrap)
.then(() => {
expect(2).to.equal(2);
return component.viewModel.activate({user: user});
});
});
it('can manually handle lifecycle', () => {
return component.manuallyHandleLifecycle().create(bootstrap)
.then(() => component.bind({user: user}))
.then(() => component.attached())
.then(() => component.unbind() )
.then(() => {
expect(component.viewModel.name).toBe(null);
return Promise.resolve(true);
});
});
afterEach( () => {
component.dispose();
});
});
下面是我遇到的错误:
1) my aurelia tests
can manually handle lifecycle:
TypeError: Cannot read property 'name' of undefined
这里是定义component
对象上的viewModel
的行,但前提是设置了aurelia.root.controllers.length
。我不知道如何在我的aurelia代码中设置控制器,或者我是否需要这样做。
我想我的问题是:如何在单元测试中访问组件的viewModel?
发布于 2018-10-26 21:49:54
为了让它发挥作用,我不得不使用Container
import { UserCard } from '../../src/modules/users/user-card';
import { Container } from 'aurelia-dependency-injection';
describe.only('some basic tests', function() {
let component, user;
before(() => {
user = new User({ id: 100, first_name: "Bob", last_name: "Schmoe", email: 'joe@schmoe.com'});
user.save();
});
beforeEach(() => {
container = new Container();
userCard = container.get( UserCard );
component = StageComponent
.withResources('modules/users/user-card')
.inView('<user-card></user-card>')
.boundTo( user );
});
it('check for ', () => {
return component.create(bootstrap)
.then(() => {
expect(2).to.equal(2);
return userCard.activate({user: user});
});
});
});
https://stackoverflow.com/questions/52975104
复制相似问题