tl; dr Not toHaveBeenCalled给我一个错误,因为先前的测试调用了此函数。
我正在尝试对我的reducer功能进行单元测试。
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;
}
}
我嘲笑console.error
let consoleErrorSpy;
beforeAll(() => {
consoleErrorSpy = jest.spyOn(global.console, 'error')
.mockImplementation(jest.fn); // mute console errors
});
我测试console.error
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();
});
但是我收到此错误“预期的模拟函数不被调用,但是它被调用为:[“动作类型:'未知'没有相应的化简器。”]“
来自先前的测试。 我可以在创建新函数之前刷新函数的调用吗?