我的一个组件中的RxJs主题的反跳时间如下所示
search = new Subject<string>();
ngOnInit() {
this.columnSearch = this.search
.pipe(debounceTime(400), distinctUntilChanged())
.subscribe((value) => {
const columnName = this.params.filterParams.colDef.field;
const searchData = {
[columnName]: value,
};
this.filterColumns(searchData, this.params.listType);
});
}
我试图为上述功能编写规格,如下所示
describe('#ngOnInit', () => {
it('should call the init', fakeAsync(() => {
spyOn(component, 'filterColumns').and.callThrough();
component.ngOnInit();
tick(400);
expect(component.filterColumns).toHaveBeenCalled();
}));
});
哪个显示类似Expected spy filterColumns to have been called.
的错误,我不确定这是否是测试主题的正确方法。谁能告诉我如何用去抖时间对主题进行单元测试。预先感谢
答案 0 :(得分:1)
如果this.search
发出一个值,则您的测试将起作用:
// Does not contain a value yet!
search = new Subject<string>();
因此,永远不会触发包含debounceTime
的管道。
使用fakeAsync
和tick(400)
进行其余测试,并且间谍定义正确。
对我来说,要达到的目标尚不完全清楚,但是在某些时候,您必须致电component.search.next(someValue)
才能使测试正常进行。