Jasmine:监视由另一个函数调用的导出函数不起作用
Jasmine : Spying an exported function that is called by an another function doesn't work
我已经创建了 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);
});
但问题是我得到了错误:
Expected spy test1 to have been called.
这不是茉莉花的问题。这就是 javascript 模块在 compiled/transpiled.
时的行为方式
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);
});
很好的article解释了上面的问题,供大家参考
我已经创建了 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);
});
但问题是我得到了错误:
Expected spy test1 to have been called.
这不是茉莉花的问题。这就是 javascript 模块在 compiled/transpiled.
时的行为方式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);
});
很好的article解释了上面的问题,供大家参考