在编写中间件测试时:
export const myMiddleware = ({getState, dispatch}) => next => action => {
console.log("action.type:", action.type) // 'NOTHING'
console.log("action.type compare:", action.type=== 'targetedAction') // this is false
next(action)
if(action.type === 'targetedAction') {
console.log("in if:" )
const state = getState()
const data = getData(state)
data.forEach( => { /*do something */ }) )
}
}
笑话代码是:
it('should pass the intercepted action to next', () => {
const fakeNext = {fakeMethod(){return true}};
const spy = jest.spyOn(fakeNext,'fakeMethod');
const fakeStore = {
dispatch: {},
getState: jest.fn()
};
const action = { type: 'NOTHING' };
myMiddleware(fakeStore)(spy)(action);
expect(spy).toHaveBeenCalledWith(action)
});
当我尝试运行测试时,它将尝试执行if条件内的代码并引发错误。由于我的测试仅是要确保next(action)已执行且action.type与预期不符,因此它不应该绕过if条件并通过测试吗?
当前,它在执行getData(state)时引发错误。