jest.fn()不被期望作为间谍函数识别?

时间:2017-07-18 22:10:42

标签: unit-testing jestjs expect.js

错误:

  

"实际" expect(actual)中的参数.toHaveBeenCalled()必须是间谍

最小的不工作示例:

const mockFn = jest.fn();
mockFn();
expect(mockFn).toHaveBeenCalled();

问题: jest.fn()不是间谍?

1 个答案:

答案 0 :(得分:0)

我尝试了以下对我有用的实现

it("should call mock function when button is clicked", () => {
    const mockFn = jest.fn();
    const tree = shallow(<button name="button test" handleHandler={mockFn} />);
    tree.simulate("click");
    mockFn();
    expect(mockFn).toHaveBeenCalled();
  });