Skip to content

Commit ac25183

Browse files
authored
Merge pull request volksbright#212 from veaba/2.0-cn
完成了Vuex testing.md翻译
2 parents 095d1ed + eabdd9b commit ac25183

File tree

1 file changed

+39
-38
lines changed

1 file changed

+39
-38
lines changed

src/vuex/testing.md

Lines changed: 39 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,15 @@ title: 测试
33
type: vuex
44
order: 13
55
---
6+
我们主要针对Vuex中的mutaions 和actions进行单元测试。
67

7-
The main parts we want to unit test in Vuex are mutations and actions.
8+
### 测试 Mutations
89

9-
### Testing Mutations
10-
11-
Mutations are very straightforward to test, because they are just functions that completely rely on their arguments. One trick is that if you are using ES2015 modules and put your mutations inside your `store.js` file, in addition to the default export, you can also export the mutations as a named export:
10+
Mutations 很容易被测试,因为它们仅仅是一些完全依赖参数的函数。小技巧是,如果你在 `store.js` 文件中定义了 mutations,并且使用 ES2015 模块功能默认输出,那么你仍然可以给 mutations 取个变量名然后把它输出去:
1211

1312
``` js
1413
const state = { ... }
15-
16-
// export mutations as a named export
14+
//取个变量名并输出mutations
1715
export const mutations = { ... }
1816

1917
export default new Vuex.Store({
@@ -22,7 +20,7 @@ export default new Vuex.Store({
2220
})
2321
```
2422

25-
Example testing a mutation using Mocha + Chai (you can use any framework/assertion libraries you like):
23+
以下为使用 Mocha + chai 测试 mutation 的例子(实际上你可以用任何你喜欢测试框架)
2624

2725
``` js
2826
// mutations.js
@@ -36,7 +34,7 @@ export const mutations = {
3634
import { expect } from 'chai'
3735
import { mutations } from './store'
3836

39-
// destructure assign mutations
37+
// 解构赋值mutations(destructure assign mutations
4038
const { increment } = mutations
4139

4240
describe('mutations', () => {
@@ -51,11 +49,14 @@ describe('mutations', () => {
5149
})
5250
```
5351

54-
### Testing Actions
52+
### 测试 Actions
53+
54+
55+
Actions 可能会更加棘手一些,因为他们可能要求请求外部API.
5556

56-
Actions can be a bit more tricky because they may call out to external APIs. When testing actions, we usually need to do some level of mocking - for example, we can abstract the API calls into a service and mock that service inside our tests. In order to easily mock dependencies, we can use Webpack and [inject-loader](https://github.com/plasticine/inject-loader) to bundle our test files.
57+
当测试actions时,我们通常需要增加mocking服务层——例如,我们可以把API调用抽象成服务,然后我们在测试中模拟这种服务。为了便于解决mock的依赖关系,可以用 Webpack [inject-loader](https://github.com/plasticine/inject-loader) 打包测试文件。
5758

58-
Example testing an async action:
59+
异步action测试示例:
5960

6061
``` js
6162
// actions.js
@@ -71,14 +72,13 @@ export const getAllProducts = ({ dispatch }) => {
7172

7273
``` js
7374
// actions.spec.js
74-
75-
// use require syntax for inline loaders.
76-
// with inject-loader, this returns a module factory
77-
// that allows us to inject mocked dependencies.
75+
// 使用 require 语法处理内联loaders
76+
//inject-loader,返回一个模块工厂
77+
//让我们能够注入mocked的依赖关系。
7878
import { expect } from 'chai'
7979
const actionsInjector = require('inject!./actions')
8080

81-
// create the module with our mocks
81+
//使用mocks创建模块
8282
const actions = actionsInjector({
8383
'../api/shop': {
8484
getProducts (cb) {
@@ -89,11 +89,11 @@ const actions = actionsInjector({
8989
}
9090
})
9191

92-
// helper for testing action with expected mutations
92+
//用指定的mutatios测试action的辅助函数
9393
const testAction = (action, args, state, expectedMutations, done) => {
9494
let count = 0
9595

96-
// mock commit
96+
// mock 提交
9797
const commit = (type, payload) => {
9898
const mutation = expectedMutations[count]
9999
expect(mutation.type).to.equal(type)
@@ -106,10 +106,10 @@ const testAction = (action, args, state, expectedMutations, done) => {
106106
}
107107
}
108108

109-
// call the action with mocked store and arguments
109+
// 用模拟的 store 和参数调用 action
110110
action({ commit, state }, ...args)
111111

112-
// check if no mutations should have been dispatched
112+
// 检查是否没有 mutation 被 dispatch
113113
if (expectedMutations.length === 0) {
114114
expect(count).to.equal(0)
115115
done()
@@ -126,11 +126,11 @@ describe('actions', () => {
126126
})
127127
```
128128

129-
### Testing Getters
129+
### 测试 Getters
130130

131-
If your getters have complicated computation, it is worth testing them. Getters are also very straightforward to test as same reason as mutations.
131+
如果你的getters方法很复杂,那么你得测试他们。测试Getter 方法和测试mutations一样非常简单!
132132

133-
Example testing a getter:
133+
测试getter实例:
134134

135135
``` js
136136
// getters.js
@@ -150,7 +150,7 @@ import { getters } from './getters'
150150

151151
describe('getters', () => {
152152
it('filteredProducts', () => {
153-
// mock state
153+
// mock 状态
154154
const state = {
155155
products: [
156156
{ id: 1, title: 'Apple', category: 'fruit' },
@@ -161,10 +161,10 @@ describe('getters', () => {
161161
// mock getter
162162
const filterCategory = 'fruit'
163163

164-
// get the result from the getter
164+
// 从getter中取回值
165165
const result = getters.filteredProducts(state, { filterCategory })
166166

167-
// assert the result
167+
// 声明返回值
168168
expect(result).to.deep.equal([
169169
{ id: 1, title: 'Apple', category: 'fruit' },
170170
{ id: 2, title: 'Orange', category: 'fruit' }
@@ -173,13 +173,14 @@ describe('getters', () => {
173173
})
174174
```
175175

176-
### Running Tests
176+
### 运行测试
177+
178+
如果你的 mutations 和 actions 已经正确,后面应该在适合的mocking上浏览器测试API的依赖关系。
177179

178-
If your mutations and actions are written properly, the tests should have no direct dependency on Browser APIs after proper mocking. Thus you can simply bundle the tests with Webpack and run it directly in Node. Alternatively, you can use `mocha-loader` or Karma + `karma-webpack` to run the tests in real browsers.
180+
#### 在Node上运行
179181

180-
#### Running in Node
182+
创建下面的webpack配置(加上适合[`.babelrc`](https://babeljs.io/docs/usage/babelrc/)):
181183

182-
Create the following webpack config (together with proper [`.babelrc`](https://babeljs.io/docs/usage/babelrc/)):
183184

184185
``` js
185186
// webpack.config.js
@@ -201,20 +202,20 @@ module.exports = {
201202
}
202203
```
203204

204-
Then:
205+
然后:
205206

206207
``` bash
207208
webpack
208209
mocha test-bundle.js
209210
```
210211

211-
#### Running in Browser
212+
#### 在浏览器上运行
212213

213-
1. Install `mocha-loader`
214-
2. Change the `entry` from the Webpack config above to `'mocha!babel!./test.js'`.
215-
3. Start `webpack-dev-server` using the config
216-
4. Go to `localhost:8080/webpack-dev-server/test-bundle`.
214+
1. 安装 `mocha-loader`
215+
2. 把上述 webpack 配置中的 entry 改成 'mocha!babel!./test.js'
216+
3. 用以上配置启动 `webpack-dev-server`
217+
4. 访问 `localhost:8080/webpack-dev-server/test-bundle`.
217218

218-
#### Running in Browser with Karma + karma-webpack
219+
#### 使用Karma + karma-webpack在浏览器中测试
219220

220-
Consult the setup in [vue-loader documentation](http://vue-loader.vuejs.org/en/workflow/testing.html).
221+
详细的安装咨询见[vue-loader documentation](http://vue-loader.vuejs.org/en/workflow/testing.html).

0 commit comments

Comments
 (0)