【发布时间】:2019-09-06 02:02:36
【问题描述】:
我有这个 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,
...
})
在我的测试文件 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)
})
})
这是我得到的结果:
突变 1) 增量
0 通过 (75ms) 1 次失败
1) 突变 增量: 类型错误:增量不是函数
在 Context.increment (dist\webpack:\tests\unit\store.spec.js:13:5)
编辑(2019 年 4 月 16 日)
我又走了一步。我在这里看到我应该“导出”我的 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
});
但即使这样......我得到了丑陋的(测试失败消息)
0 通过 (61ms) 1 次失败
1) 突变 增量: TypeError:增量不是函数 在 Context.increment (dist\webpack:\tests\unit\store.spec.js:12:5)
【问题讨论】:
标签: javascript unit-testing vue.js vuex