开玩笑的功能必须是我的模拟或间谍

时间:2018-02-23 14:19:55

标签: javascript reactjs redux enzyme jest

我正在编写测试,我在我的应用中测试操作。我在试图获得最后期望被召唤时遇到了麻烦。

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

});

但是我在运行测试后得到的消息是

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

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

1 个答案:

答案 0 :(得分:2)

您应该使用jest.spyOn(object, methodName)来创建Jest模拟功能。例如:

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();
});
相关问题