繁体   English   中英

尝试单元测试商店,如何使其工作?

[英]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:增量不是函数

在Context.increment(dist \\ webpack:\\ tests \\ unit \\ store.spec.js:13:5)

编辑(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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM