小编Man*_*n J的帖子

如何使用 toHaveBeenCalledWith 检查匿名函数的函数参数?

我想测试一个函数,它只返回一个带有一些值的函数和一个匿名函数作为参数。如何在玩笑中使用 toHaveBeenCalledWith 测试匿名函数的匹配?

function toBeTested( id, values) {
 return xyz(id, values, () => {
  return { 
    type: 'foo', 
    payload: { 
     text: values.text
    }
  }
 })
}
Run Code Online (Sandbox Code Playgroud)

在我的测试中

describe('test for toBeTested',  () => {
  it('should call xyz with params', () => {
    const id = 123;
    const values = {
      text: 'Hello world',
    };

   xyz = jest.fn();
   toBeTested(id, values);
    expect(xyz).toHaveBeenCalledWith(id, values, () => {
     return {
      type: 'foo',
      payload: {
       text: values.text,
      }
     }
    });
  })
})
Run Code Online (Sandbox Code Playgroud)

测试错误报告

 expect(jest.fn()).toHaveBeenCalledWith(expected)

    Expected mock …
Run Code Online (Sandbox Code Playgroud)

jestjs enzyme

12
推荐指数
1
解决办法
1万
查看次数

标签 统计

enzyme ×1

jestjs ×1