我的 Angular 项目中有一个 utils.ts 文件,其中:
export const testFn = function(): void { };
export const testFn1 = function(): void {
testFn();
};
我需要测试 node.js 中的功能(而不是在浏览器中)
我编写这样的测试:
describe('Тестирование функции testFn1', () => {
let moduleObj: any = null;
beforeEach(() => {
moduleObj = { testFn, testFn1 };
});
it('должна выполниться без ошибок', () => {
expect(moduleObj.testFn1).not.toThrow();
});
it('должна вызывать функцию testFn', () => {
spyOn<any>(moduleObj, 'testFn');
moduleObj.testFn1();
expect(moduleObj.testFn).toHaveBeenCalled();
});
});
但是第二次测试失败了,Error: Expected spy testFn to have been called
Jasmine没有看到testFn函数调用,我做错了什么?
简而言之:
函数 testFn1 可以在有或没有其他函数的情况下实现,并且逻辑可以保持不变。您需要查看函数本身的工作,它接受什么,返回什么,而不是它内部发生了什么,谁调用了那里的内容。