【问题标题】:Trying unit testing store, How to make it work?尝试单元测试商店,如何使其工作?
【发布时间】: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


    【解决方案1】:

    我终于找到了这个答案: 在我的测试文件中,我有

    import { expect } from 'chai'
    import mutations from '@/store'
    

    导入突变的正确方法应该是......

    import { expect } from 'chai'
    import {mutations} from '@/store'
    

    此案已由 { }

    解决:)

    问候,

    【讨论】:

      猜你喜欢
      • 2014-11-15
      • 2017-05-24
      • 2022-11-11
      • 1970-01-01
      • 2020-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-08
      相关资源
      最近更新 更多