我试图模拟我的axios函数,它总是返回空:
LoginAction:
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 []为什么接收返回为空?
发布于 2020-04-24 13:41:19
在你的快递中你写道
loginUser({email:"test", password:"test"})但是,您的操作不需要对象,它需要2个参数
export function loginUser(email, password) {我是这样命名它的:
loginUser("test", "test")希望有人会觉得这很有用
https://stackoverflow.com/questions/59568221
复制相似问题