【发布时间】:2018-08-29 08:58:00
【问题描述】:
我有 2 个函数,一个调用另一个,另一个返回一些东西,但我无法让测试工作。
使用expect(x).toHaveBeenCalledWith(someParams); 需要使用间谍,但我不知道如何在同一文件中窥探函数...
错误: : 预期是间谍,但得到了函数。
用法:expect().toHaveBeenCalledWith(...arguments)
Example.ts
doSomething(word: string) {
if (word === 'hello') {
return this.somethingElse(1);
}
return;
}
somethingElse(num: number) {
var x = { "num": num };
return x;
}
Example.spec.ts
fake = {"num": "1"};
it('should call somethingElse', () => {
component.doSomething('hello');
expect(component.somethingElse).toHaveBeenCalledWith(1);
});
it('should return object', () => {
expect(component.somethingElse(1)).toEqual(fake);
});
【问题讨论】:
标签: javascript angular typescript testing karma-runner