VUE测试UTILS:如何将Vuelidate验证规则传递给儿童组件?
在尝试通过使用 vue test utils
,测试儿童组件和其他内容之间的互动来编写组件测试
时,由于儿童组件中的Vuelidate使用,我被卡住了。以下是一个简化的示例:
// parent component code
<template>
<div>
<childA />
</div>
</template>
//childA code
<template>
<input v-model="value" />
</template>
<script>
...
validations: {
value: {
required
}
}
...
</script>
// parent component test
...
const wrapper = mount(MyParentComponent, {
...,
components: {
childA,
},
validations: {
value: required
},
...
})
我试图找到可以安装的解决方案(请注意,我也想安装子组件,因此 shack> shallow-mount
不是我想要的)儿童组成部分,具有各自的Vuelixed验证规则,但我仍然没有找到任何解决方案。
取而代之的是,我的测试给了我类似的错误:
Cannot read property `value` of undefined
这很有意义,因为该测试无法访问子组件的 $ v
实例。
到目前为止有人取得了成就吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要回答您的问题,并且在进行了一些测试之后,我相信您错过了
数据
Mount
shallowmount
:不会渲染子组件myParentComponent
需要在选项中具有您的子女组件的结构,所以这就是为什么他正在返回错误,我看到您'重新传递组件的导入,但不要忘记您的测试文件夹不在您的
src
文件夹从“@/components/childa”;
不将不会相反,我建议直接使用绝对路径来导入您的孩子组件或使用配置来解决它们
For answering your question and after i've did some test i believe you missed the
data
part inside yourmount
mount
: render child componentsshallowMount
: doesn't render child componentsMyParentComponent
need to have in the options the structure of you're child component so this is why he is returning the errorAnd i saw that you're passing the import of your component directly but don't forget that your test folder is outside of your
src
folderimport ChildA from "@/components/ChildA";
will not work instead i propose to use absolute path directly to import your child component or use a configuration to resolve them