我正在编写针对React Redux中异步操作的测试,为了解决问题,我在这里简化了代码。以下是我的action函数:
export function updateUserAuthenticationStatus(){
return function(dispatch){
return axios.get(getLoginStatusUrl())
.then(response => {
const middlewares = [thunk];
const mockStore = configureMockStore(middlewares);
const store = mockStore();
return store.dispatch(updateUserAuthenticationStatus()).then(()=>{
//expect(store.getActions()[0]).to.eql(expectedActions);
});
});
}).catch(function(response){
});
}
}
问题在于getLoginStatusUrl()函数,它会检查cookie并根据一些条件返回适当的url。所以我想模拟这个函数,例如返回test.com,然后我可以按照以下方式测试我的操作:
it("", () => {
**here I want to mock getLoginStatusUrl() to return test.com**
nock("test.com")
.get("/")
.reply(200,"test detail");
})
在这种情况下,我该如何模拟 getLoginStatusUrl() 返回 test.com?
onGet()
。这在自述文件中有说明。 - Martin Dawson