Vuejs2 是否有方法将实例添加到Vue测试UTIL中已安装的Vue组件?
我对Vue组件进行了一些单元测试。在大多数测试中,浅装组件只需要计算属性。但对于两个测试,我需要一个存储(Vuex)实例。是否有方法将实例添加到已安装的组件中?可能下面的代码有助于理解。这只是一个例子。多谢各位Vuejs2 是否有方法将实例添加到Vue测试UTIL中已安装的Vue组件?,vuejs2,vue-test-utils,Vuejs2,Vue Test Utils,我对Vue组件进行了一些单元测试。在大多数测试中,浅装组件只需要计算属性。但对于两个测试,我需要一个存储(Vuex)实例。是否有方法将实例添加到已安装的组件中?可能下面的代码有助于理解。这只是一个例子。多谢各位 describe(('Vue component test') => { beforeEach(() => { wrapper = shallowMount(Component, { computed: { errors: () =&g
describe(('Vue component test') => {
beforeEach(() => {
wrapper = shallowMount(Component, {
computed: {
errors: () => null,
isLoading: () => false,
}
})
});
describe('Test 1', => {
it('is a vue instance', () => {
expect(wrapper...);
});
});
describe('Test 2' => {
it('is a vue component', () => {
expect(wrapper...);
});
})
describe('Test with store instance', {
// Add store(Vuex) instance to the `wrapper` defined inside beforeEach() above
// Then use the wrapper
expect(wrapper...);
});
});
你所说的商店实例是什么意思?Vuex.Store的实例
如果是这样,您可以尝试:
import Vuex from 'vuex';
beforeEach(() => {
const store = new Vuex.Store({
// mock your store data here
});
wrapper = shallowMount(Component, {
store,
// your other properties, e.g. computed
});
});
如上所述,我只想在上一次测试中使用store实例,同时使用上面代码示例中已定义的shallowMounted
wapper
。但我不清楚store instance是什么意思。我对问题做了一些更改。我指的是Vuex实例。谢谢你也同意我的建议。每次测试挂载组件时都注入存储实例是没有问题的(因为你说你只需要在最后一次测试中使用它……这是一种我已经知道的方法。我在多个测试中使用同一个包装器,但我不需要在所有测试中都使用Vuex实例,所以我想知道我是否可以只在需要它的测试中,而不是在所有测试中,将Vuex实例附加到包装器上。总之(谢谢你的回答。)