Angular Jasmine:监视由另一个函数调用的导出函数不会';行不通
我创建了两个助手函数,其中一个是第一个的快捷方式,在我的测试中,我想验证是否调用了该函数,这些函数位于同一文件中:Angular Jasmine:监视由另一个函数调用的导出函数不会';行不通,angular,typescript,jasmine,karma-jasmine,Angular,Typescript,Jasmine,Karma Jasmine,我创建了两个助手函数,其中一个是第一个的快捷方式,在我的测试中,我想验证是否调用了该函数,这些函数位于同一文件中: 导出函数test1(param1、param2、param3、param4){ 返回{param1,param2,…(param3&&{param3}),…(param4&&{param4}); } 导出函数test2(param1,param2){ 返回test1(param1,null,null,param2); } 在测试中,我需要证明第一个被第二个调用: import*a
导出函数test1(param1、param2、param3、param4){
返回{param1,param2,…(param3&&{param3}),…(param4&&{param4});
}
导出函数test2(param1,param2){
返回test1(param1,null,null,param2);
}
在测试中,我需要证明第一个被第二个调用:
import*as-Util from./my-Util file';
const test2=Util.test2;
...
它('调用test2时应调用test1',()=>{
const test1=spyOn(Util,'test1')。和.callThrough();
测试2(“测试”,1);
expect(test1).toHaveBeenCalledWith('test',null,null,1);
});
或
从“/myutil文件”导入{test1,test2};
...
它('调用test2时应调用test1',()=>{
const test1Spy=jasmine.createSpy('test1');
测试2(“测试”,1);
expect(test1Spy).toHaveBeenCalledWith('test',null,null,1);
});
或
import*as-Util from./my-Util file';
const test2=Util.test2;
...
它('调用test2时应调用test1',()=>{
const test1Spy=spyOnProperty(Util,'test1');
测试2(“测试”,1);
expect(test1Spy).toHaveBeenCalledWith('test',null,null,1);
});
或
从“/myutil文件”导入{test1,test2};
...
它('调用test2时应调用test1',()=>{
const test1Spy=spyOn(窗口为任何“test1”);
测试2(“测试”,1);
expect(test1Spy).toHaveBeenCalledWith('test',null,null,1);
});
但问题是我犯了错误:
预期已调用spy test1
这不是茉莉花的问题。这就是javascript模块在编译/传输时的行为方式
var test1 = function test1() {};
var test2 = function test2() { test1(); };
exports.test1 = test1;
exports.test2 = test2;
声明函数test2
时,函数声明中会包含对test1
函数的引用。但在规范中,我们真正导入的是exports.test1
&&&exports.test2
。因此,我们基本上是在监视一些非预期的东西,即exports.test1
因此,为了保存引用,我们可以定义并导出测试函数,如下所示:
const test1 = function(param1: any, param2: any, param3: any, param4: any) {
return {
param1,
param2,
...(param3 && { param3 }),
...(param4 && { param4 })
};
};
const test2 = function(param1: any, param2: any) {
return testFunctions.test1(param1, null, null, param2); //Here we are attaching the refernce
};
export const testFunctions = {
test1,
test2
};
import * as test from './test';
const functions = test.testFunctions;
it('should call test1 when test2 is called', () => {
const test1 = spyOn(functions, 'test1').and.callThrough();
functions.test2('test', 1);
expect(test1).toHaveBeenCalledWith('test', null, null, 1);
});
我们可以通过以下方式对其进行测试:
const test1 = function(param1: any, param2: any, param3: any, param4: any) {
return {
param1,
param2,
...(param3 && { param3 }),
...(param4 && { param4 })
};
};
const test2 = function(param1: any, param2: any) {
return testFunctions.test1(param1, null, null, param2); //Here we are attaching the refernce
};
export const testFunctions = {
test1,
test2
};
import * as test from './test';
const functions = test.testFunctions;
it('should call test1 when test2 is called', () => {
const test1 = spyOn(functions, 'test1').and.callThrough();
functions.test2('test', 1);
expect(test1).toHaveBeenCalledWith('test', null, null, 1);
});
一个很好的例子,解释了上面的问题供您参考我认为它可能与您导入
Util
文件的方式有关。是否可以尝试正常导入这两个函数<代码>从“/myutil文件”导入{test1,test2}代码>我也试过了,但还是出现了同样的错误