【发布时间】:2018-01-19 17:06:41
【问题描述】:
sub: Subject = new Subject();
function funToTest() {
const localSubscription = this.sub.subscribe(() => {
this.otherFunctionToBeCalled();
localSubscription.unsubscribe();
});
}
如何测试otherFunctionToBeCalled()是否被调用?
it('should otherFunctionToBeCalled be called', () => {
theComponent.funToTest();
// doesn't work
expect(theComponent.otherFunctionToBeCalled).toHaveBeenCalled();
----------------------------------------------------------------
theComponent.sub.next('something');
theComponent.funToTest();
// doesn't work
expect(theComponent.otherFunctionToBeCalled).toHaveBeenCalled();
----------------------------------------------------------------
theComponent.funToTest();
// doesn't work
theComponent.sub.subscribe(() => {
expect(theComponent.otherFunctionToBeCalled).toHaveBeenCalled();
});
----------------------------------------------------------------
spyOn(theComponent.sub, 'subscribe').and.callFake(() => {});
theComponent.funToTest();
// doesn't work
expect(theComponent.otherFunctionToBeCalled).toHaveBeenCalled();
});
【问题讨论】:
标签: javascript typescript testing jasmine rxjs