我正在尝试嘲笑我的回答,但我无法设法使它起作用。我有以下测试,我认为该测试应覆盖此页面底部代码中的Axios.get,并简单地返回true。
import actions from '../store/dashboard/actions';
describe('actions', () => {
it('getSprintInfo', async () => {
let _url = 'url of the route';
let _body = {
team: 'unexpectables'
};
jest.mock("axios", () => ({
get: (_url, _body) => {
return new Promise((resolve) => {
resolve(true)
})
}
}));
const commit = jest.fn();
const state = jest.fn();
await actions.getSprintInfo( { commit, state} );
expect(commit).toHaveBeenCalledWith("SET_SPRINT", true);
});
});
这是我要测试的代码: 因此,我认为axios.get不应在测试时执行,并且响应应具有我在jest.mock中设置的值TRUE
export default {
getSprintInfo: ({ commit, state }) => {
commit('SET_SPRINT', {
name: 'Laden...',
goal: 'Laden...',
});
axios
.get(settings.routes.route('v1.team.home', {
team: state.team.name,
}))
.then((response) => {
commit('SET_SPRINT', response.data);
})
.catch((error) => {
console.log(error);
});
},
即时消息错误是无法读取未定义的属性名称,因此它仍然执行axios.get
有人可以帮我吗?
我遵循了以下教程:https://lmiller1990.github.io/vue-testing-handbook/vuex-actions.html#testing-for-the-api-error