teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

3

期待結果

2020/04/16 01:50

投稿

sanezane
sanezane

スコア91

title CHANGED
File without changes
body CHANGED
@@ -10,7 +10,7 @@
10
10
 
11
11
  # ソース
12
12
  #### スペックファイル
13
- - `callApi `の中の`setResult`呼ばれるかテストしようとしています。
13
+ - `callApi `を叩いて結果stateに格納さているかテストしようとしています。
14
14
 
15
15
  ```call
16
16
  import MockAdapter from 'axios-mock-adapter'

2

期待結果を変更

2020/04/16 01:50

投稿

sanezane
sanezane

スコア91

title CHANGED
File without changes
body CHANGED
@@ -1,17 +1,13 @@
1
1
  # やりたいこと
2
- #### specファイルからmock APIを叩いて想定通りにミューテーション呼ばれるか確認したい
2
+ #### specファイルからmock APIを叩いて想定通りにstore.stateに値格納さているか確認したい
3
3
 
4
4
  ### 使用ライブラリ
5
5
  #### ・axios-mock-adapter(モック)
6
6
 
7
7
  # 現状
8
- #### スペックファイルからアクションを呼ぶことはできているが、アクション内this.$axios.$postできない。
8
+ #### state中身
9
9
 
10
- # エラー文
11
- #### `Type Error Cannot read property '$post' of undefined`
12
- ![イメージ説明](0dfba86cac429b0272da7adb5cc5ddd4.png)
13
10
 
14
-
15
11
  # ソース
16
12
  #### スペックファイル
17
13
  - `callApi `の中の`setResult`が呼ばれるかをテストしようとしています。
@@ -19,25 +15,57 @@
19
15
  ```call
20
16
  import MockAdapter from 'axios-mock-adapter'
21
17
  import axios from 'axios'
18
+ import Vuex from 'vuex'
19
+ import { createLocalVue } from '@vue/test-utils'
20
+ import { cloneDeep } from 'lodash'
22
- import { actions } from '../../store/api/call.js'
21
+ import { actions, mutations } from '../../store/api/call.js'
23
22
  import CERTIFY_INFO from '../../plugins/mockData/certify/certifyInfo'
23
+
24
+ const state = {
25
+ api: {}
26
+ }
27
+
28
+ // beforeEachで毎回Storeを生成するために。
29
+ const initStore = () => {
30
+ return cloneDeep({
31
+ state,
32
+ mutations,
33
+ actions
34
+ })
35
+ }
36
+
24
37
  describe('callApiModule/actions', () => {
38
+ // eslint-disable-next-line no-unused-vars
39
+ let store
40
+ let localVue
41
+ beforeEach(() => {
42
+ // eslint-disable-next-line prefer-const
43
+ localVue = createLocalVue()
44
+ localVue.use(Vuex)
45
+ // eslint-disable-next-line prefer-const
46
+ store = new Vuex.Store(initStore())
47
+ })
48
+
25
49
  const mock = new MockAdapter(axios)
26
50
  afterEach(() => {
27
51
  mock.reset()
28
52
  })
53
+
54
+ mock.onPost('/api/call').reply(200, CERTIFY_INFO)
55
+
29
56
  describe('callApiModule/actions', () => {
30
- test('callApi commits setResult mutation', async () => {
57
+ it('call action', async () => {
31
- const context = jest.fn()
58
+ const commit = jest.fn()
59
+
32
- await actions.callApi(context)
60
+ // await actions.callApi({ commit })
33
- expect(context.commit).toHaveBeenCalledWith('setResult', CERTIFY_INFO)
61
+ await store.dispatch('api/call/callApi')
62
+ expect(store.state.api).toBe(CERTIFY_INFO.result)
34
63
  })
35
64
  })
36
65
  })
37
66
  ```
38
67
 
39
68
  ### callApi
40
- - `this.$axios.$post`の部分でエラーとなります。
41
69
 
42
70
  ```
43
71
  export const actions = {
@@ -69,7 +97,7 @@
69
97
  }
70
98
  ```
71
99
 
72
- # テストではなくて通常の処理については想定の結果が得られております。
100
+ # 通常の処理(テストじゃなく)については想定の結果が得られております。
73
101
 
74
102
  # テストではaxiosそのものは呼べないからaxiosをモック化する必要がある??
75
103
  - ↑の方法は現在調査中です。

1

タグ追加

2020/04/16 01:48

投稿

sanezane
sanezane

スコア91

title CHANGED
File without changes
body CHANGED
File without changes