You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/vuex/testing.md
+39-38Lines changed: 39 additions & 38 deletions
Original file line number
Diff line number
Diff line change
@@ -3,17 +3,15 @@ title: 测试
3
3
type: vuex
4
4
order: 13
5
5
---
6
+
我们主要针对Vuex中的mutaions 和actions进行单元测试。
6
7
7
-
The main parts we want to unit test in Vuex are mutations and actions.
8
+
### 测试 Mutations
8
9
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:
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.
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.
0 commit comments