VueJS事件回调间谍不起作用

时间:2017-04-11 16:02:28

标签: javascript vue.js karma-runner

我有一个VueJS组件text-editor,我在测试中称之为

import Vue from 'vue';
import TextEditor from '../src/TextEditor.vue';
import expect, {spyOn} from 'expect';

describe("<text-editor>", (done) => {
    beforeEach(() => {
        this.vm = new Vue({
            data: { hi: { text: "hi" } },
            template: '<div><text-editor ref="texteditor" v-model="hi" @hide="hidecb"></text-editor></div>',
            components: { 'text-editor': TextEditor },
            methods: {
                hidecb: function() {
                    console.log('hide');
                }
            }
        }).$mount();
    });

    it("should call the @hide callback", (done) => {
        let spy = spyOn(this.vm, 'hidecb');

        // This call will trigger an this.$emit('hide') in the component
        this.vm.$refs.texteditor.save();

        Vue.nextTick(() => {
            expect(spy).toHaveBeenCalled();
            done();
        });
    });
});

奇怪的是,方法hidecb确实被调用,我在控制台上看到“隐藏”。但间谍会因以下错误而失败:

Error: spy was not called (undefined:29)

我检查过并且在nextTick之前确实调用了回调。我还应该指出,我正在使用Webpack,expectjs和业力。

有人能够指出为什么间谍不起作用吗?

0 个答案:

没有答案