[英]Testing Vue components methods
I am starting with Vue.js and is really hard to find documentation about Unit Test . 我从Vue.js开始,很难找到有关单元测试的文档。
I am trying to test components methods
and builtin stuff as ready()
. 我试图测试组件
methods
和内置的东西作为ready()
。 I can call those correctly but they internally have references to this
object and this
context is lost during testing time. 我可以正确地调用它们,但它们在内部引用了
this
对象,并且在测试时间内丢失了this
上下文。
error 错误
TypeError: this.$on is not a function
spec.js 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 component.vue
export default {
name: 'Partners',
data() {
return { };
},
methods: {
get() {
// ...
}
},
ready() {
this.$on('confirm', (confirm) => {
// ...
});
this.get();
}
};
I think ready() is depreciated along with Vue 1.0. 我认为ready()与Vue 1.0一起折旧。 Consider upgrading to Vue 2 (where mounted() replaces ready()).
考虑升级到Vue 2(其中mounted()替换ready())。
To test your component, you need to initialize your component and a Vue instance, (and usually mount it, depending what you are doing). 要测试组件,需要初始化组件和Vue实例(并且通常根据您的操作安装它)。
Using the vue-webpack template (which uses Vue 2): 使用vue-webpack模板(使用Vue 2):
var ctor = Vue.extend(Partners)
var vm = new ctor()
vm.$mount()
now you can do things like vm.method(), and vm.mount() will automatically be called, etc. 现在你可以做像vm.method()这样的事情,并且会自动调用vm.mount()等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.