Redux 如何刷新模拟fn呼叫?

Redux 如何刷新模拟fn呼叫?,redux,jestjs,Redux,Jestjs,tl;dr Not TohaveEncall给了我一个错误,因为之前的测试调用了这个函数 function reducer(previousState, action) { const { type } = action; switch (type) { case '1': return {}; case '2': return {}; default: if (!type.startsWith('@@redux')) cons

tl;dr Not TohaveEncall给了我一个错误,因为之前的测试调用了这个函数

function reducer(previousState, action) {
  const { type } = action;
  switch (type) {
    case '1':
      return {};
    case '2':
      return {};
    default:
      if (!type.startsWith('@@redux')) console.error(`Action type: '${type}' has no corresponding reducer.`);
      return previousState;
  }
}
我正在尝试对我的减速机功能进行单元测试

function reducer(previousState, action) {
  const { type } = action;
  switch (type) {
    case '1':
      return {};
    case '2':
      return {};
    default:
      if (!type.startsWith('@@redux')) console.error(`Action type: '${type}' has no corresponding reducer.`);
      return previousState;
  }
}
我模拟控制台错误

let consoleErrorSpy;
beforeAll(() => {
  consoleErrorSpy = jest.spyOn(global.console, 'error')
    .mockImplementation(jest.fn); // mute console errors
});
 it('should print a console error if unknown action was given', () => {
    reducer({}, { type: 'unknown' });
    expect(consoleErrorSpy.mock.calls[0][0])
      .toBe(`Action type: 'unknown' has no corresponding reducer.`);
  });
我测试控制台。错误

let consoleErrorSpy;
beforeAll(() => {
  consoleErrorSpy = jest.spyOn(global.console, 'error')
    .mockImplementation(jest.fn); // mute console errors
});
 it('should print a console error if unknown action was given', () => {
    reducer({}, { type: 'unknown' });
    expect(consoleErrorSpy.mock.calls[0][0])
      .toBe(`Action type: 'unknown' has no corresponding reducer.`);
  });
紧接着,我测试了if的情况

  it('should not print a console error, if action came from redux internals', () => {
    reducer({}, { type: '@@redux/INTERNAL_ACTION' });
    expect(consoleErrorSpy).not.toHaveBeenCalled();
  });
但我得到了这个错误“预期不会调用模拟函数,但调用它时使用:[”操作类型:“未知”没有相应的减缩器。“”

来自上一次测试。
我可以在创建新函数之前刷新函数调用吗?

您需要在每次测试之前清除模拟:

consoleErrorSpy.mockClear()

更多信息请参见。

您需要在每次测试之前清除模拟:

consoleErrorSpy.mockClear()

更多信息请访问。

谢谢。那正是我要找的。谢谢。这正是我要找的。