使用NUXT.JS和Jest测试对同一组件中其他功能的调用
我正在尝试编写一个测试,该测试将各种方法调用到同一文件中的其他方法。到目前为止,我已经为两种不同的方法编写了测试,一种通过,一个没有。
async updateOnDeleteImage() {
try {
this.getAllAvailableImages()
let result = await SliderService.getSlideInformation(this.currentSlide)
this.slideData = result
} catch (error) {
console.error(error)
} finally {
this.autoUnpublishOnChange()
this.refreshPreviewBox()
}
}
这是迄今为止的测试文件:
import { shallowMount, createLocalVue } from '@vue/test-utils'
import ControlsBoxRight from '@/components/pages/slider-manager/controls-box-right'
import Vuetify from 'vuetify'
import Vuex from 'vuex'
let vuetify
describe('Test slider manager controls box', () => {
const localVue = createLocalVue()
let wrapper
localVue.use(Vuex)
const currentSlideMock = jest.fn()
currentSlideMock.mockReturnValue(1)
const store = new Vuex.Store({
getters: {
getSelectedSlide: currentSlideMock
}
})
beforeEach(() => {
vuetify = new Vuetify()
})
test('updateOnDeleteImage calls getAllAvailableImages', async () => {
wrapper = shallowMount(ControlsBoxRight, {
localVue,
store,
propsData: {
slideId: 1
},
})
wrapper.vm.getAllAvailableImages = jest.fn()
wrapper.vm.updateOnDeleteImage()
wrapper.vm.$nextTick()
expect(wrapper.vm.getAllAvailableImages.mock.calls.length).toBe(1)
})
test('updateOnDeleteImage calls get autoUnpublishOnChange', async () => {
wrapper = shallowMount(ControlsBoxRight, {
localVue,
store,
propsData: {
slideId: 1
},
})
wrapper.vm.autoUnpublishOnChange = jest.fn()
wrapper.vm.updateOnDeleteImage()
wrapper.vm.$nextTick()
expect(wrapper.vm.autoUnpublishOnChange.mock.calls.length).toBe(1)
})
})
GetallavailableImages通过的测试通过,AutounPublishOnChange测试不在同一文件中。我不确定为什么第二次没有通过。
此外,NUXT结构文件系统允许我创建进行实际API调用的服务文件,然后我导入这些服务文件以访问API调用。我很难弄清楚如何模拟对服务文件的呼叫。
我问两个问题,因为它们都发生在updateOndeNeTEIMAGE方法中
I'm trying to write a test that tests the various method calls to other methods within the same file. So far, I've written tests for two different methods, one passes, one does not.
async updateOnDeleteImage() {
try {
this.getAllAvailableImages()
let result = await SliderService.getSlideInformation(this.currentSlide)
this.slideData = result
} catch (error) {
console.error(error)
} finally {
this.autoUnpublishOnChange()
this.refreshPreviewBox()
}
}
and here is the test file thus far:
import { shallowMount, createLocalVue } from '@vue/test-utils'
import ControlsBoxRight from '@/components/pages/slider-manager/controls-box-right'
import Vuetify from 'vuetify'
import Vuex from 'vuex'
let vuetify
describe('Test slider manager controls box', () => {
const localVue = createLocalVue()
let wrapper
localVue.use(Vuex)
const currentSlideMock = jest.fn()
currentSlideMock.mockReturnValue(1)
const store = new Vuex.Store({
getters: {
getSelectedSlide: currentSlideMock
}
})
beforeEach(() => {
vuetify = new Vuetify()
})
test('updateOnDeleteImage calls getAllAvailableImages', async () => {
wrapper = shallowMount(ControlsBoxRight, {
localVue,
store,
propsData: {
slideId: 1
},
})
wrapper.vm.getAllAvailableImages = jest.fn()
wrapper.vm.updateOnDeleteImage()
wrapper.vm.$nextTick()
expect(wrapper.vm.getAllAvailableImages.mock.calls.length).toBe(1)
})
test('updateOnDeleteImage calls get autoUnpublishOnChange', async () => {
wrapper = shallowMount(ControlsBoxRight, {
localVue,
store,
propsData: {
slideId: 1
},
})
wrapper.vm.autoUnpublishOnChange = jest.fn()
wrapper.vm.updateOnDeleteImage()
wrapper.vm.$nextTick()
expect(wrapper.vm.autoUnpublishOnChange.mock.calls.length).toBe(1)
})
})
the test for getAllAvailableImages passes, the autoUnpublishOnChange test does not, both are within the same file. I'm not sure why the second isn't passing.
Additionally, the way Nuxt structures the file system allows me to create service files that make the actual api calls, and I then import those service files to access the api calls. I'm having trouble figuring out how to mock a call to a service file.
I ask both questions because they both happen within the updateOnDeleteImage method
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我发现了。
第一个问题,我重组以分开关注。第二个
是一个例子:
I figured it out.
The first problem, I restructured to separate away concerns. for the second
here is an example: