[英]Mock an axios call in a vue method using Jest
I have looked for a solution to this but other questions dont really match my use case.我一直在寻找解决方案,但其他问题并不真正符合我的用例。 I have a function in a Vue file:
我在 Vue 文件中有一个 function:
getStatus () {
const statusRetriveKey = 0
// Sets parameters from external file
const url = serverDetails.url
const params = { ...serverDetails.params }
axios
.get(`${url}admin/${statusRetriveKey}`, {
params
})
.then((response) => {
this.systemStatus = response.data[0].systemStatus
this.alert = response.data[0].alert
this.awayMessage = response.data[0].awayMessage
})
// Catch and display errors
.catch((error) => {
this.error = error.toString()
console.log('Error on check System status: ' + error)
})
},
I am trying to write a unit test in Jest that will check that the data variables;我正在尝试在 Jest 中编写一个单元测试来检查数据变量; systemStatus,alert and awayMessage are set correctly if that is what is returned by the GET request.
systemStatus、alert 和 awayMessage 如果这是 GET 请求返回的内容,则设置正确。
So far I have the following:到目前为止,我有以下内容:
it("Should call axios and return data", async () => {
mockAxios.get.mockImplementationOnce(() => Promise.resolve({
data:{
systemStatus: true,
//the other variables
}
}))
const response = await wrapper.vm.getStatus();
console.log(response)
expect(wrapper.vm.$data.systemStatus).toBe(true)
expect(mockAxios.get).toHaveBeenCalledTimes(1);
})
However I am receiving for the system status variable:但是我收到系统状态变量:
expect(received).toBe(expected) // Object.is equality
Expected: true
Received: null
I know that the mock is not feeding data into the method but not sure on how to resolve.我知道模拟没有将数据输入到方法中,但不确定如何解决。 Is it because I am not explicitly returning from the method in the vue file and rather using this to set the data values?
是不是因为我没有从 vue 文件中的方法显式返回,而是使用它来设置数据值?
Solution:解决方案:
// Import dependencies
import { shallowMount } from '@vue/test-utils'
import axios from 'axios'
// Import component
import Page from 'path'
jest.spyOn(axios, "get").mockImplementation(() => Promise.resolve({ data: [{ systemStatus: false, ...:..., ...:... }] }));
// Set Model data
const models = {
...
}
}
describe('Page', () => {
let wrapper
beforeEach(() => {
wrapper = shallowMount(Page, {
propsData: {
model: models
},
})
})
it("Should call axios and return data", async () => {
await wrapper.vm.getStatus();
expect(wrapper.vm.$data.systemStatus).toBe(false)
expect(wrapper.vm.$data....).toBe('...')
expect(wrapper.vm.$data....).toBe('...')
},)
})
So not sure what is mockAxios here, is it some npm module?所以不确定这里的mockAxios是什么,是npm模块吗?
One way you could do it is using spyOn一种方法是使用spyOn
import axios from 'axios';
jest.spyOn(axios, "get").mockImplementationOnce(() => Promise.resolve({data: {/* Here you can add your data */}));
So in your test, notice mockImplementationOnce .因此,在您的测试中,请注意mockImplementationOnce 。 It will be mocked only for one call
它只会被一个电话嘲笑
it("Should call axios and return data", async () => {
await wrapper.vm.getStatus();
})
Edit:编辑:
On the line where you log response you will always get undefined because you do not have return statement in the function getStatus.在您记录响应的行上,您将始终未定义,因为您在 function getStatus 中没有返回语句。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.