【发布时间】:2018-01-21 15:09:41
【问题描述】:
我正在为我在 react redux 中的异步操作编写测试,为了解决我在此处简化代码的问题。这是我的动作函数:
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?
【问题讨论】:
标签: reactjs redux react-redux jestjs reactjs-testutils