Javascript Jasmine test if方法仅当内部方法是函数实例时才调用另一个方法
我需要使用jasmine@2.99.1 来自my-component.ts的代码Javascript Jasmine test if方法仅当内部方法是函数实例时才调用另一个方法,javascript,angular,unit-testing,jasmine,karma-jasmine,Javascript,Angular,Unit Testing,Jasmine,Karma Jasmine,我需要使用jasmine@2.99.1 来自my-component.ts的代码 startChatting(agent, platform) { if (this.params.startChatting instanceof Function) { this.params.startChatting(agent, platform, this.params.rowIndex); } } 我尝试测试上面的代码:my-component.spec.ts it('ensure t
startChatting(agent, platform) {
if (this.params.startChatting instanceof Function) {
this.params.startChatting(agent, platform, this.params.rowIndex);
}
}
我尝试测试上面的代码:my-component.spec.ts
it('ensure that startChatting does not call "params.startChatting" if "params.startChatting"
is not instanceOf Function', () => {
component.params = {
startChatting: null,
rowIndex: 2
}
spyOn(component.params, 'startChatting');
component.startChatting('dummyId', 'telegram');
expect(component.params.startChatting).not.toHaveBeenCalled();
});
但是测试失败,并显示消息“Error:Expected spy startChatting not called.”这意味着调用了内部方法
因此,我尝试将我在测试用例开始时设置为null的内部方法记录到控制台日志中,但我发现它不是null,而是如下所示:
ƒ () { return fn.apply(this, arguments); }
我知道在spyOn函数被调用后,情况发生了变化
所以我的问题是如何测试这种情况?我需要确保,如果params.startChatting不是函数的实例,就不会被调用
提前感谢此情况无法解决,因为您没有要测试的功能。然而,这可能对你有用
let startChattingGetterInwoked = 0;
component.params = {
get startChatting() {
startChattingGetterInwoked++;
return null;
},
rowIndex: 2
}
component.startChatting('dummyId', 'telegram');
expect(startChattingGetterInwoked).toBe(1);
// Not sure how offten it is called, but at least one should be called due to `typeof`
您无法测试此案例。该错误是由于类型为
Jasmine.spy
的null not导致的。。。您可以通过getter在startChatting参数上进行测试感谢@Akxe为您的重播您能解释一下如何通过getter进行测试,或者给我一个链接来解释一下吗