在 Vue 中,我想检查我的商店中的一个动作是否正确地使用 Jest 调用另一个动作spyOn
,我尝试了不同的方式,但它似乎不起作用,这是我的代码:
// index.js
getRecipes ({ dispatch }) {
const fruits = ['apple', 'banana', 'pear']
fruits.forEach((fruit) => {
dispatch('getRecipe', fruit)
})
},
async getRecipe ({ commit }) {
const recipe = await recipesService.fetchRecipe(payload)
commit(SET_RECIPE, { recipe })
},
// index.spec.js
test('getRecipes calls getRecipe 3 times, each with the right fruit', () => {
const commit = jest.fn()
const dispatch = jest.fn()
const spy = spyOn(actions, 'getRecipe')
const result = actions.getRecipes({ commit, dispatch })
expect(spy).toHaveBeenCalledTimes(3)
expect(spy).toHaveBeenCalledWith('apple')
})
但是当我运行测试时,这是我得到的输出:
Expected spy to have been called three times, but it was called zero times.
我还有其他地方想测试这种集成(一个动作调用另一个),但它仍然给我这个错误。