我正在为Vue组件编写测试,该组件的子组件正在使用find /home -type f '(' -name "*.zip" -o -name "*.rar" ')' '!' -path "01_Personal Folder*" -printf '%P %k\n'
文件而不是.svg
文件。
我的问题是,触发该子组件的点击不会注册,并且间谍也不会记录任何后续函数调用。
我正在使用vue-test-utils,mocha和sinon。
MyComponent.vue
.vue
我的测试文件MyComponent.spec.js
<template>
<ChildComponent
@click="doSomething"
data-test-id="open"
/>
</template>
<script>
import ChildComponent from "@/assets/imgs/something.svg" //Component is an svg file
export default {
name: "MyComponent",
methods: {
doSomething() {
return 'foo'
}
}
}
</script>
我尝试使用import { shallowMount } from "@vue/test-utils";
import { expect } from "chai";
import sinon from "sinon";
import MyComponent from "@/components/myComponent"
import ChildComponent from "@/assets/imgs/something.svg"
describe("MyComponent", () => {
const component = shallowMount(MyComponent, {})
const mockMethod = sinon.spy()
const childComponent = component.find("[data-test-id='open']")
component.setMethods({
doSomething: mockMethod
})
childComponent.trigger("click")
// childComponent.vm.$emit("click") //throws error 'TypeError: Cannot read property '$emit' of undefined'
expect(childComponent.exists()).to.be.true // Passes
expect(mockMethod).to.have.been.called; // Fails. with message: 'expected spy to have been called at least once, but it was never called'
})
触发点击,但这告诉我子组件childComponent.vm.$emit("click")
尚未定义。我认为这可能与vm
是一个功能组件或不是childComponent
文件这一事实有关。
我也尝试过.vue
,它不会出错,但不会触发函数调用。
任何建议,不胜感激!