我一直试图模拟我的 axios 函数,该函数总是返回空:
登录操作:
export function loginUser(email, password) {
return function(dispatch) {
return axios
.post(`${API_URL}/users/authenticate`, { email, password }, { withCredentials: true })
.then(response => {
localStorage.setItem("token", JSON.stringify(response.data.user.id));
dispatch(setupUser(response.data.user.id));
dispatch({ type: AUTH_USER });
dispatch({ type: CLEAR_LOGIN_MESSAGE });
});
};
}
测试:
describe("async actions", () => {
beforeEach(() => {
moxios.install();
});
afterEach(() => {
moxios.uninstall();
});
it("logs in", () => {
moxios.wait(() => {
const request = moxios.requests.mostRecent();
request.respondWith({
status: 200,
response: {name:"Jill", id:1},
});
});
const expectedActions = [
{ type: AUTH_USER },
{ type: CLEAR_LOGIN_MESSAGE },
{ type: SET_UP_USER, information: {name:"Jill, id:1} },
{ type: REFRESH_DASHBOARD, dashboard: "" },
];
const store = mockStore({});
return store.dispatch(loginUser({email:"test", password:"test"})).then(() => {
expect(store.getActions()).toEqual(expectedActions);
});
});
});
我得到:
expect(received).toEqual(expected) // deep equality
- Expected
+ Received
- Array [
- Object {
- "type": "AUTH_USER",
- },
- Object {
- "type": "CLEAR_LOGIN_MESSAGE",
- },
- Object {
- "information":
- "id": "1",
- "name": "Jill",
- },
- "type": "SET_UP_USER",
- },
- Object {
- "dashboard": "",
- "type": "CLEAR_DASHBOARD",
- },
- ]
+ Array []
为什么received返回是空的?