• 首页
  • vue
  • TypeScript
  • JavaScript
  • scss
  • css3
  • html5
  • php
  • MySQL
  • redis
  • jQuery
  • 对 Vuex 中的 mutation 和 action 进行单元测试

    我们主要想针对 Vuex 中的mutationaction进行单元测试。


    测试 Mutation

    Mutation很容易被测试,因为它们仅仅是一些完全依赖参数的函数。这里有一个小技巧,如果你使用了 ES2015 模块,且将mutation定义在了store.js文件中,那么除了模块的默认导出外,你还应该将mutation进行命名导出:

    const state = { ... }
    
    // `mutations` 作为命名输出对象
    export const mutations = { ... }
    
    export default createStore({
      state,
      mutations
    })
    

    下面是用Mocha+Chai测试一个mutation的例子(实际上你可以用任何你喜欢的测试框架):

    // mutations.js
    export const mutations = {
        increment: state => state.count++
    }
    
    // mutations.spec.js
    import { expect } from 'chai'
    import { mutations } from './store'
    
    // 解构 `mutations`
    const { increment } = mutations
    
    describe('mutations', () => {
      it('INCREMENT', () => {
        // 模拟状态
        const state = { count: 0 }
        // 应用 mutation
        increment(state)
        // 断言结果
        expect(state.count).to.equal(1)
      })
    })
    


    测试 Action

    Action应对起来略微棘手,因为它们可能需要调用外部的 API。当测试action的时候,我们需要增加一个 mocking 服务层——例如,我们可以把 API 调用抽象成服务,然后在测试文件中用 mock 服务回应 API 调用。为了便于解决 mock 依赖,可以用 webpack 和 inject-loader 打包测试文件。

    下面是一个测试异步action的例子:

    // actions.js
    import shop from '../api/shop'
    
    export const getAllProducts = ({ commit }) => {
      commit('REQUEST_PRODUCTS')
      shop.getProducts(products => {
        commit('RECEIVE_PRODUCTS', products)
      })
    }
    
    // actions.spec.js
    
    // 使用 require 语法处理内联 loaders。
    // inject-loader 返回一个允许我们注入 mock 依赖的模块工厂
    import { expect } from 'chai'
    const actionsInjector = require('inject-loader!./actions')
    
    // 使用 mocks 创建模块
    const actions = actionsInjector({
      '../api/shop': {
        getProducts (cb) {
          setTimeout(() => {
            cb([ /* mocked response */ ])
          }, 100)
        }
      }
    })
    
    // 用指定的 mutations 测试 action 的辅助函数
    const testAction = (action, args, state, expectedMutations, done) => {
      let count = 0
    
      // 模拟提交
      const commit = (type, payload) => {
        const mutation = expectedMutations[count]
    
        try {
          expect(mutation.type).to.equal(type)
          expect(mutation.payload).to.deep.equal(payload)
        } catch (error) {
          done(error)
        }
    
        count++
        if (count >= expectedMutations.length) {
          done()
        }
      }
    
      // 用模拟的 store 和参数调用 action
      action({ commit, state }, ...args)
    
      // 检查是否没有 mutation 被 dispatch
      if (expectedMutations.length === 0) {
        expect(count).to.equal(0)
        done()
      }
    }
    
    describe('actions', () => {
      it('getAllProducts', done => {
        testAction(actions.getAllProducts, [], {}, [
          { type: 'REQUEST_PRODUCTS' },
          { type: 'RECEIVE_PRODUCTS', payload: { /* mocked response */ } }
        ], done)
      })
    })
    

    如果在测试环境下有可用的 spy(比如通过 Sinon.JS),你可以使用它们替换辅助函数testAction

    describe('actions', () => {
      it('getAllProducts', () => {
        const commit = sinon.spy()
        const state = {}
    
        actions.getAllProducts({ commit, state })
    
        expect(commit.args).to.deep.equal([
          ['REQUEST_PRODUCTS'],
          ['RECEIVE_PRODUCTS', { /* mocked response */ }]
        ])
      })
    })
    


    测试 Getter

    如果你的 getter 包含很复杂的计算过程,很有必要测试它们。Getter 的测试与 mutation 一样直截了当。

    测试一个 getter 的示例:

    // getters.js
    export const getters = {
      filteredProducts (state, { filterCategory }) {
        return state.products.filter(product => {
          return product.category === filterCategory
        })
      }
    }
    
    // getters.spec.js
    import { expect } from 'chai'
    import { getters } from './getters'
    
    describe('getters', () => {
      it('filteredProducts', () => {
        // 模拟状态
        const state = {
          products: [
            { id: 1, title: 'Apple', category: 'fruit' },
            { id: 2, title: 'Orange', category: 'fruit' },
            { id: 3, title: 'Carrot', category: 'vegetable' }
          ]
        }
        // 模拟 getter
        const filterCategory = 'fruit'
    
        // 获取 getter 的结果
        const result = getters.filteredProducts(state, { filterCategory })
    
        // 断言结果
        expect(result).to.deep.equal([
          { id: 1, title: 'Apple', category: 'fruit' },
          { id: 2, title: 'Orange', category: 'fruit' }
        ])
      })
    })
    


    执行测试

    如果你的 mutation 和 action 编写正确,经过合理地 mocking 处理之后这些测试应该不依赖任何浏览器 API,因此你可以直接用 webpack 打包这些测试文件然后在 Node 中执行。换种方式,你也可以用mocha-loader或 Karma +karma-webpack在真实浏览器环境中进行测试。

    在 Node 中执行测试

    创建以下 webpack 配置(配置好.babelrc):

    // webpack.config.js
    module.exports = {
      entry: './test.js',
      output: {
        path: __dirname,
        filename: 'test-bundle.js'
      },
      module: {
        loaders: [
          {
            test: /\.js$/,
            loader: 'babel-loader',
            exclude: /node_modules/
          }
        ]
      }
    }
    

    然后:

    webpack
    mocha test-bundle.js
    

    在浏览器中测试

    1. 安装mocha-loader
    2. 把上述 webpack 配置中的entry改成'mocha-loader!babel-loader!./test.js'
    3. 用以上配置启动webpack-dev-server
    4. 访问localhost:8080/webpack-dev-server/test-bundle

    上篇:热重载

    下篇:vuex 与 组合式 API