【发布时间】:2020-01-06 12:42:32
【问题描述】:
我的组件的 ngOnInit 这样做
public ngOnInit() {
this.ctrSubscription = combineLatest(this.route.queryParams, this.tags$).subscribe(async params => {
if (Object.keys(params[0]).length > 0) {
this.initView({ ...params[0] });
}
if (Object.keys(params[1]).length > 0) {
this.wsTags = Object.values(params[1]);
}
if (params[0] && this.wsTags.length > 0) {
this.searchWs({ ...params[0] }.q);
this.pruneData();
}
});
}
这就是我测试它的方式
it('should perform search and load the results', fakeAsync(() => {
TestBed.get(ActivatedRoute).queryParams = of({ q: 'test' });
const initSpy = spyOn<any>(component, 'initView').and.callThrough();
const subSpy = spyOn<any>(component.tags$, 'subscribe');
component.wsTags = ensureState(mockTags as any);
flushMicroTasks();
fixture.detectChanges();
flushMicroTasks();
expect(initSpy).toHaveBeenCalledWith({ q: 'test' });
expect(subSpy).toHaveBeenCalled();
expect(component.wsTags).toBe(Object.values(mockTags));
}));
spys 根本没有被调用。我在运行测试时设置了一个断点以了解该值是否通过,并且我能够捕获正确传递的查询参数,但 if 仍然失败。
我尝试阅读文档以更好地理解堆栈溢出以及此处有关堆栈溢出的各种类似问题,但我无法解决它。我对编写单元测试相当陌生,因此将不胜感激。谢谢。
【问题讨论】:
标签: angular unit-testing testing rxjs karma-jasmine