vue.js - [Vue 警告]:挂载钩子错误:“TypeError:this.$refs.appConfirm.setCellphone 不是函数”
问题描述
我在尝试模拟 vue 中的引用时出错,因为我找到的解决方案使用了存根。但是存根只接受字符串。
it("Test component", () => {
const wrapper = mount(NeedConfirmation, {
localVue,
store
});
});
[Vue 警告]:挂载钩子错误:“TypeError:this.$refs.appConfirm.setCellphone 不是函数”
it("Test component", () => {
const appConfirm = {
setCellphone: () => {}
};
const wrapper = mount(NeedConfirmation, {
localVue,
store,
stubs: {
appConfirm
}
});
});
[vue-test-utils]:options.stub 值必须传递一个字符串或组件
解决方案
对于自定义存根组件,提供组件定义stubs
,例如,通过导入'*.vue'
文件或使用 Vue 选项 API 定义组件对象。
以下示例定义了 Vue 2 1️⃣ 中的组件内联,并验证setCellphone
在挂载 2️⃣ 后调用该组件:
import NeedConfirmation from '@/components/NeedConfirmation.vue'
import { mount } from '@vue/test-utils'
it('Test component', () => {
const setCellphone = jest.fn()
const wrapper = mount(NeedConfirmation, {
stubs: {
// 1️⃣
appConfirm: {
template: `<div></div>`,
methods: {
setCellphone
}
}
}
})
// wait for refs to resolve in `mount()`...
await wrapper.vm.$nextTick()
// 2️⃣
expect(setCellphone).toHaveBeenCalled()
})
推荐阅读
- webpack - 单个 spa cli 不要求 angular 框架 mfe 的组织名称
- csv - golang csv.write 不写但没有错误
- amazon-web-services - Docker 映像中的交互式 shell 与 Amazon ECS 与 `aws ecs run-task` 后跟 `aws ecs execute-command`
- python - $query = http_build_query($params); $auth_hash = hash_hmac('sha512', $query, $secret_key);
- excel - 在没有 VBA 的 Excel 中从多个工作表中过滤行
- json - 如何使用这种 json 格式将国家和城市呼叫到我的下拉列表中
- numpy-ndarray - 结构 Numpy Arrays 错误,结合数组和 dtype
- java - 访问同步属性后,两个线程可以同时访问同一个方法吗?
- python - 大熊猫中日期时间与固定偏移量的日期时间
- python - 如何从确实页面中获取整数