【发布时间】:2016-07-15 08:30:05
【问题描述】:
我正在对一个组件进行单元测试,这就是我所拥有的:
describe('Component: nav', () => {
beforeEach(() => addProviders([
provide(UserService, {useClass: MockUserService}),
NavComponent
]));
it('should have a property \'firstName\' with the value \'Crazypug\'',
async(inject([NavComponent], (component) => {
component.ngOnInit();
expect(component.firstName).toEqual('Crazypug')
}))
);
});
这是 ngOnInit 函数:
ngOnInit() {
this.userService.getFirstNameCall().subscribe(
data => {this.firstName = data.firstname; });
}
当我运行测试时,我得到:
预期未定义等于“Crazypug”
如何让 Jasmine 等待 ngOnInit 函数完成? 我在 SO 上找到了一些解决方案,但从很久以前(以 angular2 时间而言)。它们非常复杂或过时。
【问题讨论】:
标签: angular jasmine karma-jasmine