我正在使用axios-mock-adapter模拟axios帖子呼叫。我可以通过添加以下内容来验证是否进行了邮寄通话:
console.log(mock.handlers.post);
测试代码中的。但是由于某种原因,其余的promise .then()语句都没有运行。
这是我的测试代码:
test('Should redirect to login when successful user register', async () => {
const historyMock = { push: jest.fn() };
const mock = new MockAdapter(axios);
mock.onPost('/users/register').reply(200, {
name: user.name,
email: user.email,
id: user._id, // eslint-disable-line no-underscore-dangle
});
await registerUser(user, historyMock);
expect(historyMock.push).toHaveBeenCalled();
});
这是实际功能:
// Register new user
export const registerUser = (user, history) => (dispatch) => {
axios
.post('/users/register', user)
.then(res => history.push('/login'))
.catch((err) => {
dispatch({
type: GET_ERRORS,
error: err.response.data,
});
});
};
运行测试时,我也会从Jest收到此错误:
Expected mock function to have been called, but it was not called.