[英]Trying unit testing store, How to make it work?
I have this store.js file 我有这个store.js文件
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
export const mutations = {
increment: state => state.count++,
Changeloading(state, status) { state.loading = status },
ChangeUserGroup(state, newGroup) { state.userGroup = newGroup; },
ChangeOriginalRole(state, newRole) { state.originalRole = newRole; }
}
export default new Vuex.Store({
state: {
count: 0,
loading: false, //header, TeamLead, TeamMember
listUserGroup: [],
userRole: "",
originalRole: "",
userGroup: {}
},
mutations,
...
})
In my testing file store.spec.js 在我的测试文件中store.spec.js
import { expect } from 'chai'
import mutations from '@/store'
// destructure assign `mutations`
const { increment } = mutations
describe('mutations', () => {
it('INCREMENT', () => {
// mock state
const state = { count: 0 }
// apply mutation
increment(state)
// assert result
expect(state.count).to.equal(1)
})
})
This is the result i am getting: 这是我得到的结果:
mutations 1) INCREMENT 变异1)增加
0 passing (75ms) 1 failing 0通过(75ms)1失败
1) mutations INCREMENT: TypeError: increment is not a function 1)突变INCREMENT:TypeError:增量不是函数
EDIT (4/16/2019) 编辑(4/16/2019)
I walk down one more step. 我又走了一步。 I saw here that I should "export" all the components in my store.js like:
我在这里看到我应该“导出” store.js中的所有组件,例如:
import Vue from 'vue';
import Vuex from 'vuex';
Vue.use(Vuex);
export const mutations = {...};
export const state = {...};
export const actions = {...};
export const getters = {...};
export default new Vuex.Store({
state,
mutations,
actions,
getters
});
But even in that way... I'm getting the ugly (testing fail message) 但是即使那样,我也变得丑陋(测试失败消息)
0 passing (61ms) 1 failing 0通过(61ms)1失败
1) mutations increment: TypeError: increment is not a function at Context.increment (dist\\webpack:\\tests\\unit\\store.spec.js:12:5) 1)突变增量:TypeError:增量不是Context.increment的函数(dist \\ webpack:\\ tests \\ unit \\ store.spec.js:12:5)
I finally found the answer for this one: In my test file I have 我终于找到了答案:在我的测试文件中
import { expect } from 'chai'
import mutations from '@/store'
right way to import mutations should be... 导入突变的正确方法应该是...
import { expect } from 'chai'
import {mutations} from '@/store'
The case was resolved :) by { } 此案由{}解决:)
Regards, 问候,
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.