我试图模拟一个获取请求,以确认是否调度了正确的操作:冗余功能:
export function loginRequest(email, password) {
return (dispatch) => {
dispatch(login(email, password))
return fetch('http://localhost:8000/login-success.json')
.then((response) => response.json())
.then((response) => dispatch(loginSuccess()))
.catch((error)=>dispatch(loginFailure()))
}
}
测试:
test("API returns the right response, the store received two actions LOGIN and LOGGING_SUCCESS", () => {
const store = mockStore({})
fetchMock.get('*', {})
return store.dispatch(loginRequest('jack', 124)).then(()=>{
const actions = store.getActions()
console.log(actions)
expect(actions).toEqual([login('jack', 124), loginSuccess()])
})
})
console.log输出:
[
{ type: 'LOGIN', user: { email: 'jack', password: 124 } },
{ type: 'LOGIN_FAILURE' }
]
我希望第二个操作是LOGIN_SCCESS操作。看起来模拟根本不起作用。,我是不是遗漏了什么
已解决:在我定义loginRequest函数的文件中,我正在导入fetch(import fetch from 'node-fetch';
(,因此在测试中调用该函数会导致实际的fetch调用,而不是fetch模拟