![](/img/trans.png)
[英]How to make material components work with Karma in unit testing Angular
[英]Trying unit testing store, How to make it work?
我有這個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)突變INCREMENT:TypeError:增量不是函數
編輯(4/16/2019)
我又走了一步。 我在這里看到我應該“導出” 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)
我終於找到了答案:在我的測試文件中
import { expect } from 'chai'
import mutations from '@/store'
導入突變的正確方法應該是...
import { expect } from 'chai'
import {mutations} from '@/store'
此案由{}解決:)
問候,
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.