我有我的前端单元测试,但是我不喜欢我必须将mockAxios
调用按测试运行时发生的顺序进行放置。在下面的示例中,我的mockAxios.post
呼叫被撤消,测试失败。有没有一种方法可以在模拟设置中指定URL,以便确保在代码运行时模拟仅适用于该特定调用?
编辑:看来我可以使用axios-mock-adapter做我想要的事情。我想知道我是否应该改变使用它。 https://github.com/ctimmerm/axios-mock-adapter
it('shows error when axios call fails', (done) => {
// ToDo: I don't like having to depend on order for mocking axios. We should be able to
// explicitly define what happens for a specific call. Look into this.
// Mock the call to get employees
mockAxios.post.mockImplementationOnce(() =>
Promise.reject({ error: 'some error message' })
);
// Mock the logging call
mockAxios.post.mockImplementationOnce(() =>
Promise.resolve({ data: '' })
);
const expected = {
type: 'SET_BANNER_VISIBILITY',
payload: {
visible: true,
type: 'error',
text: `We're having trouble loading employees. Please try again in a few minutes. ` +
`If the problem still exists, please contact support.`,
showSupportButton: true
}
};
return mockStore.dispatch(actions.getEmployees()).then(() => {
expect(mockStore.getActions().pop()).toEqual(expected);
done();
});
});
答案 0 :(得分:0)
是否可以选择使用额外的库:https://github.com/ctimmerm/axios-mock-adapter?
似乎在开玩笑地为该功能开了罚单:https://github.com/facebook/jest/issues/6180