简体   繁体   English

jest 函数必须是模拟或间谍

[英]jest function must be a mock or spy

I'm writing a test where Im testing the actions in my app.我正在编写一个测试,我在我的应用程序中测试操作。 I'm having trouble trying to get the last expect to be called.我在尝试获得最后一个被调用的期望时遇到了麻烦。

const pushData = jest.fn(() => Promise.resolve());

test('anotherAsyncCall is fired to get more info', () => {
  const store = mockStore({});
  asynCallToGetData = jest.fn(() => Promise.resolve());

  const action = pushData();
  const dispatch = jest.fn();
  const anotherAsyncCall = jest.fn(() => Promise.resolve());

  const expectedActions = [{
    type: 'DATA_RECEIVED_SUCCESS'
  }];

  return store.dispatch(action).then(() => {
    expect(asynCallToGetData).toHaveBeenCalled();
    expect(store.getActions()).toMatch(expectedActions[0].type);
    expect(dispatch(anotherAsyncCall)).toHaveBeenCalled(); //This fails
  });
});

But the message I get after running test is但是我在运行测试后得到的消息是

expect(jest.fn())[.not].toHaveBeenCalled()

jest.fn() value must be a mock function or spy.
  Received: undefined here

You should use jest.spyOn(object, methodName) to create Jest mock function.您应该使用jest.spyOn(object, methodName)来创建 Jest 模拟函数。 For example:例如:

const video = require('./video');

test('plays video', () => {
  const spy = jest.spyOn(video, 'play');
  const isPlaying = video.play();

  expect(spy).toHaveBeenCalled();
  expect(isPlaying).toBe(true);

  spy.mockReset();
  spy.mockRestore();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM