【发布时间】:2016-10-19 23:50:33
【问题描述】:
我从 Vue.js 开始,很难找到关于 Unit Test 的文档。
我正在尝试测试组件methods 和内置的东西ready()。我可以正确调用它们,但它们在内部引用了this 对象,并且this 上下文在测试期间丢失。
错误
TypeError: this.$on is not a function
spec.js
import Vue from 'vue';
import Partners from 'components/main/partner/Partners';
describe.only('Partners.vue', () => {
it('should render with mocked partners', (cb) => {
Partners.ready(); // I get an error here because ready() is calling inside: this.$on(...)
cb(null);
});
});
component.vue
export default {
name: 'Partners',
data() {
return { };
},
methods: {
get() {
// ...
}
},
ready() {
this.$on('confirm', (confirm) => {
// ...
});
this.get();
}
};
【问题讨论】:
标签: javascript unit-testing vue.js