Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/475.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用Jest测试是否使用定义的参数(toHaveBeenCalledWith)调用函数_Javascript_Unit Testing_Jestjs - Fatal编程技术网

Javascript 如何使用Jest测试是否使用定义的参数(toHaveBeenCalledWith)调用函数

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

我想测试一下,在我的测试中是否调用了特定的函数,参数是否正确。从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);
  }

  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(); });