Javascript 如何使用Jest测试是否使用定义的参数(toHaveBeenCalledWith)调用函数
我想测试一下,在我的测试中是否调用了特定的函数,参数是否正确。从JEST文档中,我无法找出正确的方法 假设我有这样的东西:Javascript 如何使用Jest测试是否使用定义的参数(toHaveBeenCalledWith)调用函数,javascript,unit-testing,jestjs,Javascript,Unit Testing,Jestjs,我想测试一下,在我的测试中是否调用了特定的函数,参数是否正确。从JEST文档中,我无法找出正确的方法 假设我有这样的东西: // add.js function child(ch) { const t = ch + 1; // no return value here. Function has some other "side effect" } function main(a) { if (a == 2) { child(a + 2); } retur
// add.js
function child(ch) {
const t = ch + 1;
// no return value here. Function has some other "side effect"
}
function main(a) {
if (a == 2) {
child(a + 2);
}
return a + 1;
}
exports.main = main;
exports.child = child;
现在进入单元测试:
一,。
我想运行main(1)
并测试它是否返回2
,是否未调用child()
二,。
然后我想运行main(2)
,它返回3
和child(4)
的语句只被调用了一次
我现在有这样的东西:
// add-spec.js
module = require('./add');
describe('main', () => {
it('should add one and not call child Fn', () => {
expect(module.main(1)).toBe(2);
// TODO: child() was not called
});
it('should add one andcall child Fn', () => {
expect(module.main(2)).toBe(3);
// TODO: child() was called with param 4 exactly once
// expect(module.child).toHaveBeenCalledWith(4);
});
});
我正在进行测试,因此非常感谢REPL中的一个工作示例 好的,我已经弄明白了。诀窍是,将函数拆分为单独的文件。因此,该代码是(并且适用于):
提取的child.js文件:
// child.js
function child(ch) {
const t = ch + 1;
// no return value here. Function has some other "side effect"
}
exports.child = child;
主测试文件:
// add-spec.js
main = require('./add').main;
child = require('./child').child;
child = jest.fn();
describe('main', () => {
it('should add one and not call child Fn', () => {
expect(main(1)).toBe(2);
expect(child).toHaveBeenCalledTimes(0);
});
it('should add one andcall child Fn', () => {
expect(main(2)).toBe(3);
expect(child).toHaveBeenCalledWith(4);
expect(child).toHaveBeenCalledTimes(1);
});
});
在我的例子中,我对angular代码也有类似的疑问,所以我有一个方法,当表单中的字段发生更改时调用它,这个方法的唯一任务是触发其他一些方法
代码摘录:
handleConnectionToLegChange(value) {
if (!isNullOrUndefined(value)) {
this.connectionsForm.controls.to.markAsDirty();
this.connectionsForm.controls.to.updateValueAndValidity();
this.connectionsForm.controls.from.markAsDirty();
this.connectionsForm.controls.from.updateValueAndValidity();
this.updateModalButtonStatus(this.connectionsSubject);
}}
所以为了测试它,我使用了这个测试用例。(我刚刚发现了5种触发方法中的2种,但在我的案例中已经足够了。)
试验提取物:
it('should execute fn handleConnectionToLegChange and check method calls if value is not null', () => {
component.connectionsForm.controls.to.updateValueAndValidity = jest.fn();
component.updateModalButtonStatus = jest.fn();
component.handleConnectionToLegChange('a');
expect(component.connectionsForm.controls.to.updateValueAndValidity).toHaveBeenCalled();
expect(component.updateModalButtonStatus).toHaveBeenCalled(); });
这对我来说很好。如果你要用jest.fn()
覆盖child
,那么你最好不需要child,对吧?@DHlavaty,诀窍是使用child=jest.fn()代码>,不要将函数拆分为单独的文件,对吗?您的主函数仍然不是纯函数。它在函数内部使用外部变量子函数
函数。如果将其作为参数传递给main
函数,则可以监视main
函数并检查是否调用了child
参数。
it('should execute fn handleConnectionToLegChange and check method calls if value is not null', () => {
component.connectionsForm.controls.to.updateValueAndValidity = jest.fn();
component.updateModalButtonStatus = jest.fn();
component.handleConnectionToLegChange('a');
expect(component.connectionsForm.controls.to.updateValueAndValidity).toHaveBeenCalled();
expect(component.updateModalButtonStatus).toHaveBeenCalled(); });