🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# 测试 我们主要想针对 Vuex 中的 mutation 和 action 进行单元测试。 ### 测试 Mutation Mutation 很容易被测试,因为它们仅仅是一些完全依赖参数的函数。这里有一个小技巧,如果你在 `store.js` 文件中定义了 mutation,并且使用 ES2015 模块功能默认输出了 Vuex.Store 的实例,那么你仍然可以给 mutation 取个变量名然后把它输出去: ``` js const state = { ... } // `mutations` 作为命名输出对象 export const mutations = { ... } export default new Vuex.Store({ state, mutations }) ``` 下面是用 Mocha + Chai 测试一个 mutation 的例子(实际上你可以用任何你喜欢的测试框架): ``` js // mutations.js export const mutations = { increment: state => state.count++ } ``` ``` js // 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](https://github.com/plasticine/inject-loader) 打包测试文件。 下面是一个测试异步 action 的例子: ``` js // actions.js import shop from '../api/shop' export const getAllProducts = ({ commit }) => { commit('REQUEST_PRODUCTS') shop.getProducts(products => { commit('RECEIVE_PRODUCTS', products) }) } ``` ``` js // 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) } } }) // 用指定的 mutaions 测试 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) if (payload) { 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](http://sinonjs.org/)),你可以使用它们替换辅助函数 `testAction`: ``` js 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 的示例: ``` js // getters.js export const getters = { filteredProducts (state, { filterCategory }) { return state.products.filter(product => { return product.category === filterCategory }) } } ``` ``` js // 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`](https://babeljs.io/docs/usage/babelrc/)): ``` js // 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/ } ] } } ``` 然后: ``` bash 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`。 #### 使用 Karma + karma-webpack 在浏览器中执行测试 详见 [vue-loader documentation](https://vuejs.github.io/vue-loader/workflow/testing.html)。