【发布时间】:2020-05-28 04:25:36
【问题描述】:
我想在我的 Angular 应用程序中对组件的 ngOnInit() 方法进行单元测试。 ngOnInit() 方法订阅 ActivatedRoute data 并调用 subscribe 方法内部的函数。下面是方法的代码:
component.ts
ngOnInit() {
this.route.data.subscribe(data => this.store.patchState(data));
}
这是我的测试:
component.spec.ts
beforeEach(() => {
fixture = TestBed.createComponent(xyzComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('#ngOnInit should call patchState', async(() => {
const spy = spyOn(store, 'patchState');
// When
component.ngOnInit();
fixture.detectChanges();
// Then
expect(spy.calls.count()).toBe(1, 'patchState has been called once.');
}));
但是,当我运行测试时,我收到一条错误消息,告诉我“从未调用过预期的 spy patchState”。
我相信我的问题来自测试的异步性质,但我不知道为什么它不起作用。
我在这里做错了什么?
谢谢!
【问题讨论】:
标签: angular unit-testing jasmine observable angular2-routing