unit-testing - 如何测试回调函数?VUE/JEST
问题描述
我不知道如何测试updateTotal
...如果requestAxios
成功返回回调函数updateTotal
,但我如何监视呢?
...methods:{
updateAll() {
const updateTotal = (request) => {
this.total = request.data.total
}
this.requestAxios(
'get',
'/api/',
{},
[updateTotal],
)
}
}...
请求Axios:
async requestAxios(
method = 'get',
url = '',
objSend = {},
successFunctions = [],
errorsFunctions = [],
formKey = 'form',
) {
let request = ''
if (method !== 'delete') {
request = await axios[method](url, objSend, this.headerRequestJson)
.then(response => this.responseRequestText(response))
.catch(errors => this.responseRequestText(errors.response));
} else {
request = await axios.delete(url, {
data: objSend,
headers: this.headerRequestJson.headers,
})
.then(response => this.responseRequestText(response))
.catch(errors => this.responseRequestText(errors.response));
}
if (request.status === 'success') {
// success callback fn
successFunctions.forEach((value) => {
value(request, formKey)
})
} else {
// errors callback fn
errorsFunctions.forEach((value) => {
value(request)
})
// adicionar erros nos campos
this.addErrors(request, formKey);
}
},
我的尝试:
test('updateTotalFinancial: ', () => {
const update = jest.fn()
const response = {
data: {
total: 100,
},
}
const requestAxios = jest.fn(() => update(response))
const wrapper = shallowMount(ModalUnderwriting, {
store,
localVue,
methods: {
requestAxios,
},
})
wrapper.setData({
total: '0',
})
wrapper.vm.updateTotalFinancial()
首先期望成功其次不是,不更新数据/变量total
expect(update).toBeCalled()
expect(wrapper.vm.total).toEqual(100)
解决方案
推荐阅读
- html - 新行上的 Flexbox 力
- visual-studio - Visual Studio 更新 (16.8.1) 导致 CI 构建失败
- java - Springfox-boot-starter swagger 即时处理
- string-literals - 在 ANTLR4 中正确捕获未闭合的字符串
- sql - Oracle - 获取行重复 n 次,在日期上具有动态值
- java - 如何在 SQL 中插入数据?
- java - 如何将多对多连接表的附加列映射到关联映射键?
- python - keras LSTM的结构图
- sdk - 在执行 CRUD 操作时遇到 Java SDK 3 问题
- python - 使用多维数组从字典创建 pd.DataFrame