【发布时间】:2017-08-26 08:30:37
【问题描述】:
我正在尝试在 npm 脚本中使用 mocha-webpack 来测试 vuejs 组件。我在测试中这样嘲笑vuex商店:
const vm = new Vue({
template: '<div><test></test></div>',
store: new Vuex.Store(mockedStore),
components: {
'test': TestItems
}
}).$mount()
我在组件上调用一个方法来测试比如:
vm.$options.components.test.methods.foo()
在组件中,我有访问商店的代码,例如:
this.$store.commit('bar', data)
问题是当它到达这一点时,我得到了这个错误:
'Cannot read property \'commit\' of undefined' undefined undefined
我已验证在此上下文中未定义“this”。当我运行应用程序时,“this”被定义并具有“$store”。如何使它在单元测试上下文中工作?
【问题讨论】:
标签: node.js unit-testing vue.js mocha.js mocha-webpack