首页 > 解决方案 > Jasmine 测试观察者的下一个是否被调用

问题描述

我有一个行为主题:

   public dataObserver = new BehaviorSubject<any>(null);

当我在订阅块中收到数据时,我接下来会调用这个:

       this.dataObserver.next(response);

我在编写单元测试用例以测试是否已调用 next 时遇到问题。请帮帮我。我试图在现有问题中查找,但没有找到任何问题。如果您有任何问题,请回复有关堆栈溢出的相关问题的链接。

标签: angularunit-testingjasmine

解决方案


尝试做:

spyOn(component.dataObserver, 'next').and.callThrough(); // and.callThrough() is optional
// put it if you want next to be called with its actual implementation
....
expect(component.dataObserver.next).toHaveBeenCalled(); // you can also use .toHaveBeenCalledWith(...);

推荐阅读