我正在开玩笑地测试我的vuex动作,但它不断向我显示这样的错误。 另一个测试可与模拟适配器完美配合,但仅此文件不会通过。 所以我认为是因为process.env。因此,我搜索stackoverflow并获得了我使用beforeEach和afterEach的答案,但仍然无法正常工作。
Expected mock function to have been called with:
"storeSearch"
as argument 1, but it was called with
"storeNews".
"peter"
as argument 2, but it was called with
undefined.
actions.vue
async updateSearch({ commit }, name) {
commit('storeSearch', name);
const url = process.env.VUE_APP_URL_API + '/food' + '?q=' + name;
const { data } = await axios.get(url);
commit('storeNews', data.result);
},
actions.spec.js
describe('', () => {
const mock = new MockAdapter(axios);
const OLD_ENV = process.env.VUE_APP_URL_API;
beforeEach(() => {
jest.resetModules();
process.env.VUE_APP_URL_API = { ...OLD_ENV };
delete process.env.VUE_APP_URL_API;
});
afterEach(() => {
process.env.VUE_APP_URL = OLD_ENV;
});
test('moxios get updateSearch', async () => {
const commit = jest.fn();
const name = 'peter';
const data = {
result: [
{
chanel: 'blah'
}
]
};
const url = OLD_ENV;
mock.onGet(url).reply(200, {
data,
name
});
await actions.updateSearch(
{
commit
},
{
name
}
);
expect(commit).toHaveBeenCalledWith('storeSearch', name);
});
});