質問編集履歴

3

期待結果

2020/04/16 01:50

投稿

sanezane
sanezane

スコア91

test CHANGED
File without changes
test CHANGED
@@ -22,7 +22,7 @@
22
22
 
23
23
  #### スペックファイル
24
24
 
25
- - `callApi `の中の`setResult`が呼ばれるかテストしようとしています。
25
+ - `callApi `を叩いて結果がstateに格納さているかテストしようとしています。
26
26
 
27
27
 
28
28
 

2

期待結果を変更

2020/04/16 01:50

投稿

sanezane
sanezane

スコア91

test CHANGED
File without changes
test CHANGED
@@ -1,6 +1,6 @@
1
1
  # やりたいこと
2
2
 
3
- #### specファイルからmock APIを叩いて想定通りにミューテーション呼ばれるか確認したい
3
+ #### specファイルからmock APIを叩いて想定通りにstore.stateに値格納さているか確認したい
4
4
 
5
5
 
6
6
 
@@ -12,15 +12,7 @@
12
12
 
13
13
  # 現状
14
14
 
15
- #### スペックファイルからアクションを呼ぶことはできているが、アクション内のthis.$axios.$postができない。
16
-
17
-
18
-
19
- # エラー文
15
+ #### stateの中身が空
20
-
21
- #### `Type Error Cannot read property '$post' of undefined`
22
-
23
- ![イメージ説明](0dfba86cac429b0272da7adb5cc5ddd4.png)
24
16
 
25
17
 
26
18
 
@@ -40,12 +32,68 @@
40
32
 
41
33
  import axios from 'axios'
42
34
 
35
+ import Vuex from 'vuex'
36
+
37
+ import { createLocalVue } from '@vue/test-utils'
38
+
39
+ import { cloneDeep } from 'lodash'
40
+
43
- import { actions } from '../../store/api/call.js'
41
+ import { actions, mutations } from '../../store/api/call.js'
44
42
 
45
43
  import CERTIFY_INFO from '../../plugins/mockData/certify/certifyInfo'
46
44
 
45
+
46
+
47
+ const state = {
48
+
49
+ api: {}
50
+
51
+ }
52
+
53
+
54
+
55
+ // beforeEachで毎回Storeを生成するために。
56
+
57
+ const initStore = () => {
58
+
59
+ return cloneDeep({
60
+
61
+ state,
62
+
63
+ mutations,
64
+
65
+ actions
66
+
67
+ })
68
+
69
+ }
70
+
71
+
72
+
47
73
  describe('callApiModule/actions', () => {
48
74
 
75
+ // eslint-disable-next-line no-unused-vars
76
+
77
+ let store
78
+
79
+ let localVue
80
+
81
+ beforeEach(() => {
82
+
83
+ // eslint-disable-next-line prefer-const
84
+
85
+ localVue = createLocalVue()
86
+
87
+ localVue.use(Vuex)
88
+
89
+ // eslint-disable-next-line prefer-const
90
+
91
+ store = new Vuex.Store(initStore())
92
+
93
+ })
94
+
95
+
96
+
49
97
  const mock = new MockAdapter(axios)
50
98
 
51
99
  afterEach(() => {
@@ -54,15 +102,25 @@
54
102
 
55
103
  })
56
104
 
105
+
106
+
107
+ mock.onPost('/api/call').reply(200, CERTIFY_INFO)
108
+
109
+
110
+
57
111
  describe('callApiModule/actions', () => {
58
112
 
59
- test('callApi commits setResult mutation', async () => {
113
+ it('call action', async () => {
60
-
114
+
61
- const context = jest.fn()
115
+ const commit = jest.fn()
62
-
116
+
117
+
118
+
63
- await actions.callApi(context)
119
+ // await actions.callApi({ commit })
64
-
120
+
65
- expect(context.commit).toHaveBeenCalledWith('setResult', CERTIFY_INFO)
121
+ await store.dispatch('api/call/callApi')
122
+
123
+ expect(store.state.api).toBe(CERTIFY_INFO.result)
66
124
 
67
125
  })
68
126
 
@@ -76,8 +134,6 @@
76
134
 
77
135
  ### callApi
78
136
 
79
- - `this.$axios.$post`の部分でエラーとなります。
80
-
81
137
 
82
138
 
83
139
  ```
@@ -140,7 +196,7 @@
140
196
 
141
197
 
142
198
 
143
- # テストではなくて通常の処理については想定の結果が得られております。
199
+ # 通常の処理(テストじゃなくについては想定の結果が得られております。
144
200
 
145
201
 
146
202
 

1

タグ追加

2020/04/16 01:48

投稿

sanezane
sanezane

スコア91

test CHANGED
File without changes
test CHANGED
File without changes