【问题标题】:How to test mutations when using vuex modules使用 vuex 模块时如何测试突变
【发布时间】:2018-05-07 08:40:50
【问题描述】:

在使用 vuex 模块之前,我的变异测试没问题:

import mutations from '@/vuex/mutations.js'
import vueAuthInstance from '@/services/auth.js'
import { IS_AUTHENTICATED, CURRENT_USER_ID } from '@/vuex/mutation_types.js'

describe('mutations.js', () => {
  var state
  beforeEach(() => {
    state = {
      isAuthenticated: vueAuthInstance.isAuthenticated(),
      currentUserId: ''
    }
  })

  describe('IS_AUTHENTICATED', () => {
    it('should set authentication status', () => {
      state.isAuthenticated = false
      mutations[IS_AUTHENTICATED](state, {isAuthenticated: true})
      expect(state.isAuthenticated).to.eql(true)
    })
  })
  ...

})

现在我重构了我的 vuex 文件夹,我的状态和突变在每个 vuex/modules/../index.js 文件中

      src
       |_  vuex
       |    L_ modules
       |           L_ login
       |               |_ index.js
       |               |_ actions.js
       |               |_ getters.js
       |               |_ mutation_types.js
       |_ App.vue
       |_ main.js

vuex/modules/login/index.js

import Vue from 'vue'
import Vuex from 'vuex'
import actions from './actions'
import getters from './getters'
import * as types from './mutation_types'
import vueAuthInstance from '@/services/auth.js'
Vue.use(Vuex)

const state = {
  isAuthenticated: vueAuthInstance.isAuthenticated(),
  currentUserId: ''
}

const mutations = {
  [types.IS_AUTHENTICATED]  (state, payload) {
    state.isAuthenticated = payload.isAuthenticated
  },
  ...
}

export default {
  state,
  mutations,
  actions,
  getters
}

使用 vuex/store.js

import Vue from 'vue'
import Vuex from 'vuex'
import login from '@/vuex/modules/login'
// import other modules

Vue.use(Vuex)

export default new Vuex.Store({
  modules: {
    login,
    ... (other modules )
  }
})

考虑到这个新结构,我重写了单元测试如下:

test/unit/specs/vuex/modules/login/index.spec.js

import { mutations } from '@/vuex/modules/login/index.js'
import vueAuthInstance from '@/services/auth.js'
import types from '@/vuex/modules/login/mutation_types.js'

describe('mutations.js', () => {
  var state
  beforeEach(() => {
    state = {
      isAuthenticated: vueAuthInstance.isAuthenticated(),
      currentUserId: ''
    }
  })

  describe('IS_AUTHENTICATED', () => {
    it('should set authentication status', () => {
      state.isAuthenticated = false
      mutations[types.IS_AUTHENTICATED](state, {isAuthenticated: true})
      expect(state.isAuthenticated).to.eql(true)
    })
  })

})

我得到一个关于突变的错误:

 ✗ should set authentication status
    TypeError: Cannot read property 'IS_AUTHENTICATED' of undefined

我尝试更改import {mutations}语句,直接导入store.js,其中定义了模块,并使用store._mutations,

LOG: 'MUTATIONS: ', Object{IS_AUTHENTICATED: [function wrappedMutationHandler(payload) { ... }], ...}

使用 store._mutations.IS_AUTHENTICATED0 ,似乎可以工作,(不知道为什么是数组?..)但是这个函数和状态,有效负载参数有问题,因为测试没有通过

import store from '@/vuex/store'
import vueAuthInstance from '@/services/auth.js'

describe('mutations.js', () => {
  var state
  beforeEach(() => {
    state = {
      isAuthenticated: vueAuthInstance.isAuthenticated(),
      currentUserId: ''
    }
  })

  describe('IS_AUTHENTICATED', () => {
    it('should set authentication status', () => {
      state.isAuthenticated = false
      console.log('MUTATIONS: ', store._mutations.IS_AUTHENTICATED())
      store._mutations.IS_AUTHENTICATED[0](state, {isAuthenticated: true})
      expect(state.isAuthenticated).to.eql(true)
    })
  })
  ...
})


1) should set authentication status
     mutations.js IS_AUTHENTICATED
     AssertionError: expected false to deeply equal true

我在 index.js 文件中检查了传递给突变的参数

const mutations = {
  [types.IS_AUTHENTICATED]  (state, payload) {
    console.log('MUTATION state: ', state)
    console.log('MUTATION payload: ', payload)
    state.isAuthenticated = payload.isAuthenticated
  },
  [types.CURRENT_USER_ID]  (state, payload) {
    state.currentUserId = payload.currentUserId
  }
}

而且。我没有看到传递的 arg 值,似乎状态 args 是我的测试中唯一传递的值:

LOG: 'MUTATION state: ', Object{isAuthenticated: false, currentUserId: ''}
LOG: 'MUTATION payload: ', Object{isAuthenticated: false, currentUserId: ''}

这段代码有什么问题?在这种情况下,如何使用 vuex 模块测试突变?

感谢反馈

【问题讨论】:

    标签: unit-testing vue.js vuex vuex-modules


    【解决方案1】:

    实际上,这是一种不好的方法。 您应该创建模拟状态并使用它。

    import { createLocalVue } from '@vue/test-utils';
    import Vuex from 'vuex';    
    import { storeModule } from '@path/to/store/modules';
    const mutations = storeModule.mutations;
    
    
    describe('mutations', () => {
      it('testCase#1', () => {
        createLocalVue().use(Vuex);
        const state = {
        //mock state values
        };
    
        const store = new Vuex.Store({state, mutations});
    
        store.commit('mutationToTest', arg);
    
        expect(state.arg).toBe(1);
      })
    })
    

    【讨论】:

      【解决方案2】:

      我找到了一种使用 vuex 模块测试突变的方法,但我不知道这是否是最好的方法...

      我的测试很简单,使用 store.commit 因为我不能直接调用突变处理程序,我只导入 vuex/store

      src/test/unit/specs/modules/login/index.js

      import store from '@/vuex/store'
      
      describe('mutations.js', () => {
        describe('IS_AUTHENTICATED', () => {
          it('should set authentication status', () => {
            store.commit('IS_AUTHENTICATED', {isAuthenticated: true})
            expect(store.state.login.isAuthenticated).to.eql(true)
          })
        })
        ...
      })
      

      【讨论】:

        猜你喜欢
        • 2017-10-13
        • 2019-05-28
        • 2021-09-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-05
        • 2021-03-29
        • 2019-04-15
        相关资源
        最近更新 更多