【发布时间】:2019-11-25 14:47:47
【问题描述】:
我已经创建了 2 个辅助函数,其中一个是第一个的快捷方式,在我的测试中我想验证这个函数是否被调用,这些函数在同一个文件中:
export function test1(param1, param2, param3, param4) {
return { param1, param2, ...(param3 && { param3 }), ...(param4 && { param4 }) };
}
export function test2(param1, param2) {
return test1(param1, null, null, param2);
}
在测试中我需要证明第一个是由第二个调用的:
import * as Util from './my-util-file';
const test2 = Util.test2;
...
it('should call test1 when test2 is called', () => {
const test1 = spyOn(Util, 'test1').and.callThrough();
test2('test', 1);
expect(test1).toHaveBeenCalledWith('test', null, null, 1);
});
或者
import {test1, test2} from './my-util-file';
...
it('should call test1 when test2 is called', () => {
const test1Spy = jasmine.createSpy('test1');
test2('test', 1);
expect(test1Spy).toHaveBeenCalledWith('test', null, null, 1);
});
或者
import * as Util from './my-util-file';
const test2 = Util.test2;
...
it('should call test1 when test2 is called', () => {
const test1Spy = spyOnProperty(Util, 'test1');
test2('test', 1);
expect(test1Spy).toHaveBeenCalledWith('test', null, null, 1);
});
或者
import {test1, test2} from './my-util-file';
...
it('should call test1 when test2 is called', () => {
const test1Spy = spyOn(window as any, 'test1');
test2('test', 1);
expect(test1Spy).toHaveBeenCalledWith('test', null, null, 1);
});
但问题是我得到了错误:
预期的 spy test1 已被调用。
【问题讨论】:
-
我认为这可能与您导入
Util文件的方式有关。您可以尝试正常导入这两个功能吗?import {test1, test2} from './my-util-file'; -
我也试过了,还是一样的错误
标签: angular typescript jasmine karma-jasmine