我正在尝试测试此功能:
function login(username, password) {
let user = { userName: username, password: password };
return dispatch => {
localStorageService.login(username, password).then((response) => {
dispatch(resetError());
dispatch(success( { type: userConstants.LOGIN, user} ));
}, (err) => {
dispatch(error(err));
});
};
function success(user) { return { type: userConstants.LOGIN, payload: user } };
};
这是我的考试
const mockStore = configureStore([thunk]);
const initialState = {
userReducer: {
loggedInUser: "",
users: [],
error: ""
}
};
const store = mockStore(initialState);
jest.mock('./../../services/localStorageService');
describe("Login action should call localstorage login", () => {
let localStorage_spy = jest.spyOn(localStorageService, 'login');
store.dispatch(userActions.login(test_data.username, test_data.password)()).then( () => {
expect(localStorage_spy).toHaveBeenCalled();
});
});
我得到的错误:
Actions must be plain objects. Use custom middleware for async actions.
许多在线资源不断告诉我在测试中使用thunk来执行这些操作,但是它不起作用。它调用的最后一件事是dispatch(resetError());
,它中断了。我从来没有真正在网上找到与我的问题足够相似的资源。我的函数返回一个调度,该调度返回一个promise,当promise解决时,该promise返回另一个调度。我只是想让函数返回。我对localStorageService.login进行了监视,并对其进行了模拟,因此我希望确保它被调用。但是当然函数不会返回