質問編集履歴

2

修正

2020/11/15 11:31

投稿

tarotarotarotar
tarotarotarotar

スコア41

test CHANGED
@@ -1 +1 @@
1
- RSpecにてコントローラーテスト中のエラー
1
+ タイプエラーが解決できない
test CHANGED
@@ -1,409 +1,89 @@
1
- 現在Rubu on Railsにてコントローラーのテストを行っております。
1
+ require 'rails_helper'
2
-
3
- 以下のエラーが解消できません。
4
2
 
5
3
 
6
4
 
7
- bundle exec rspec spec/requests/nutritions_spec.rbの実行結果
8
-
9
-
10
-
11
- ``` 1) NutritionsController GET #index indexアクションにリクエストするとレスポンスに登録済みのカロリーが存在する
12
-
13
- Failure/Error: expect(response.body).to include @nutrition.calorie
14
-
15
-
16
-
17
- TypeError:
18
-
19
- no implicit conversion of Float into String
20
-
21
- # ./spec/requests/nutritions_spec.rb:21:in `block (3 levels) in <top (required)>'
22
-
23
-
24
-
25
- コード
26
-
27
- ```
28
-
29
- nutritionsコントローラーというもののテストコードを記述しております。その際に以下のようなテストコードを書いております。そのうち、「indexアクションにリクエストするとレスポンスに登録済みの食品名が存在する」「indexアクションにリクエストするとレスポンスに登録済みのカロリーが存在する」にて同一のエラー分が返ってきます。
30
-
31
-
32
-
33
- 【nutritions_spec.rb】
34
-
35
- ```require 'rails_helper'
36
-
37
- describe NutritionsController, type: :request do
5
+ RSpec.describe 'ユーザー新規登録', type: :system do
38
-
39
-
40
6
 
41
7
  before do
42
8
 
43
- @nutrition = FactoryBot.create(:nutrition)
9
+ @user = FactoryBot.build(:user)
44
10
 
45
11
  end
46
12
 
13
+ context 'ユーザー新規登録ができるとき' do
47
14
 
15
+ it '正しい情報を入力すればユーザー新規登録ができてトップページに移動する' do
48
16
 
17
+ # トップページに移動する
49
18
 
19
+ visit root_path
50
20
 
21
+ # トップページにサインアップページへ遷移するボタンがあることを確認する
51
22
 
23
+ expect(page).to have_content('Sign Up')
52
24
 
53
- describe "GET #index" do
25
+ # 新規登録ページへ移動する
54
26
 
55
- it "indexアクションにリクエストすると正常にレスポンスが返ってくる" do
27
+ visit new_user_registration_path
56
28
 
57
- get root_path
29
+ # ユーザー情報を入力する
58
30
 
31
+ fill_in 'Nickname', with: @user.nickname
32
+
33
+ fill_in 'Email', with: @user.email
34
+
35
+ fill_in 'Password', with: @user.password
36
+
37
+ fill_in 'Password confirmation', with: @user.password_confirmation
38
+
39
+ # サインアップボタンを押すとユーザーモデルのカウントが1上がることを確認する
40
+
41
+ expect{
42
+
43
+ find('input[name="commit"]').click
44
+
45
+ }.to change { User.count }.by(1)
46
+
47
+ # トップページへ遷移する
48
+
59
- expect(response.status).to eq 200
49
+ expect(current_path).to eq root_path
50
+
51
+ # カーソルを合わせるとログアウトボタンが表示されることを確認する
52
+
53
+ expect(
54
+
55
+ find(".user_nav").fins("span").hover
56
+
57
+ ).to have_content('Log Out')
58
+
59
+ # サインアップページへ遷移するボタンや、ログインページへ遷移するボタンが表示されていないことを確認する
60
+
61
+ expect(page).to have_no_content('Sign Up')
62
+
63
+ expect(page).to have_no_content('Log In')
60
64
 
61
65
  end
62
66
 
63
- it "indexアクションにリクエストするとレスポンスに登録済みの食品名が存在する" do
67
+ end
64
68
 
65
- get root_path
69
+ context 'ユーザー新規登録ができないとき' do
66
70
 
67
- expect(response.body).to include @nutrition.ingredient
71
+ it '誤った情報ではユーザー新規登録ができずに新規登録ページへ戻ってくる' do
68
72
 
69
- end
73
+ # トップページに移動する
70
74
 
71
- it "indexクションにリクエストするとレスポスに登録済みのカロリー存在する" do
75
+ # トップページにサインップページへ遷移するボタンがあることを確認する
72
76
 
73
- get root_path
77
+ # 新規登録ページへ移動する
74
78
 
75
- expect(response.body).to include @nutrition.calorie
79
+ # ユーザー情報を入力する
76
80
 
77
- end
81
+ # サインアップボタンを押してもユーザーモデルのカウントは上がらないことを確認する
78
82
 
79
- it "indexアクションにリクエストするとレスポンスに登録済みのタンパク質が存在する" do
80
-
81
- get root_path
83
+ # 新規登録ページへ戻されることを確認する
82
-
83
- expect(response.body).to include @nutrition.protein
84
-
85
- end
86
-
87
-
88
-
89
- it "indexアクションにリクエストするとレスポンスに投稿検索フォームが存在する" do
90
84
 
91
85
  end
92
86
 
93
87
  end
94
88
 
95
89
  end
96
-
97
-
98
-
99
- コード
100
-
101
- ```
102
-
103
-
104
-
105
- float型であることに文句を言われているのでFactoriesが怪しいなと思い、色々型を変えてみたのですが、解決できませんでした。
106
-
107
- 【factories/nutritions.rb】
108
-
109
- ```FactoryBot.define do
110
-
111
- factory :nutrition do
112
-
113
-
114
-
115
- ingredient {Faker::Lorem.sentence}
116
-
117
- calorie {"300"}
118
-
119
- protein {"300"}
120
-
121
- lipid {"300"}
122
-
123
- carbohydrate {"300"}
124
-
125
- potassium {"300"}
126
-
127
- calcium {"300"}
128
-
129
- iron {"300"}
130
-
131
- vitamin_a {"300"}
132
-
133
- vitamin_b1 {"300"}
134
-
135
- vitamin_b2 {"300"}
136
-
137
- vitamin_c {"300"}
138
-
139
- dietary_fiber {"300"}
140
-
141
- association :user
142
-
143
- end
144
-
145
- end
146
-
147
- コード
148
-
149
- ```
150
-
151
-
152
-
153
- 対象のindex.html.erbは以下です。
154
-
155
-
156
-
157
- ```<div class="index-contents">
158
-
159
- <div class="index-image">
160
-
161
- <%= image_tag ('/index3.jpg') %>
162
-
163
- </div>
164
-
165
- </div>
166
-
167
- <div class="index-text">
168
-
169
- Beriiesは食材の各栄養素を管理できるアプリです。<br>
170
-
171
- 食材一覧からお好みの食材の栄養素をチェックしてみましょう。
172
-
173
- </div>
174
-
175
-
176
-
177
-
178
-
179
- <%= form_with(url: search_nutritions_path, local: true, method: :get, class: "search-form") do |form| %>
180
-
181
- <%= form.text_field :keyword, placeholder: "さっそく食材を検索しよう!", class: "search-input" %>
182
-
183
- <%= form.submit "Search", class: "search-btn" %>
184
-
185
- <% if user_signed_in? %>
186
-
187
- <div>検索しても無ければここから食材を登録しよう!</div>
188
-
189
- <div class="index-new">
190
-
191
- <%= link_to "食品登録", new_nutrition_path, class: "new-btn" %>
192
-
193
- </div>
194
-
195
- <% else %>
196
-
197
- <div class="index-new">
198
-
199
- <p class="letter-spacing"> 登録がなかったら新規登録してみましょう!<br>初めての方はここからかんたんログイン!</p>
200
-
201
- <%= link_to 'かんたんログイン', users_guest_sign_in_path, method: :post, class: "new-btn"%>
202
-
203
- <p class="login-select"> 登録済みの方  新規会員登録</p>
204
-
205
- <%= link_to "Log In", new_user_session_path, class: "new-btn2" %>
206
-
207
- <%= link_to "Sign Up", new_user_registration_path, class: "new-btn2" %>
208
-
209
- </div>
210
-
211
- <% end %>
212
-
213
-
214
-
215
-
216
-
217
- <% end %>
218
-
219
-
220
-
221
- <script src="delete.js"></script>
222
-
223
-
224
-
225
- <div class='main-contents'>
226
-
227
-
228
-
229
- <h1 class="contents-title">食品一覧<EM STYLE="font-size: large;">(100gあたり)</EM></h1>
230
-
231
- <table border="5" width="80%" cellpadding="20" bordercolor="#882d91" class="contents-column" align="left">
232
-
233
- <tr class="column-name">
234
-
235
- <th width="30%" height="50">食品名</th>
236
-
237
- <th class="column-color1" width="15%">エネルギー(kcal)</th>
238
-
239
- <th class="column-color1" width="15%">タンパク質(g)</th>
240
-
241
- <th class="column-color1" width="15%">脂質(g)</th>
242
-
243
- <th class="column-color1" width="15%">炭水化物(g)</th>
244
-
245
- <th class="column-color2" width="15%">カリウム(mg)</th>
246
-
247
- <th class="column-color2" width="15%">カルシウム(mg)</th>
248
-
249
- <th class="column-color2" width="15%">鉄(mg)</th>
250
-
251
- <th class="column-color3" width="15%">ビタミンA(mg)</th>
252
-
253
- <th class="column-color3" width="15%">ビタミンB1(mg)</th>
254
-
255
- <th class="column-color3" width="15%">ビタミンB2(mg)</th>
256
-
257
- <th class="column-color3" width="15%">ビタミンC(mg)</th>
258
-
259
- <th width="15%">食物繊維(g)</th>
260
-
261
- <%#<th width="15%">登録ユーザー</th>%>
262
-
263
- </tr>
264
-
265
-
266
-
267
- <% @nutritions.each do |nutrition| %>
268
-
269
- <tr height="60" class="content-post">
270
-
271
- <%# サインインしているかつ、お気に入り登録している食品は★がつき、カラーは赤になる%>
272
-
273
- <% if user_signed_in? %>
274
-
275
- <% if current_user.already_favorited?(nutrition, current_user) %>
276
-
277
- <td class="ingredient-column fav-color" id="ingredient-column">★<%= nutrition.ingredient %>
278
-
279
- <% else %>
280
-
281
- <td class="ingredient-column" id="ingredient-column"><%= nutrition.ingredient %>
282
-
283
- <% end %>
284
-
285
- <% else %>
286
-
287
- <td class="ingredient-column" id="ingredient-column"><%= nutrition.ingredient %>
288
-
289
- <% end %>
290
-
291
- <div class="more" id="more">
292
-
293
- <ul class="more-list" id="more-list">
294
-
295
- <li>
296
-
297
- <% if user_signed_in? %>
298
-
299
- <% if current_user.already_favorited?(nutrition, current_user) %>
300
-
301
- <%# 解除の引数としてid:0を渡しているがこれが無いとidがないというエラーが起きてしまうので応急処置 %>
302
-
303
- <%= link_to '解除', nutrition_favorites_path(user_id:current_user.id, nutrition_id:nutrition.id, id:0), :style=>"color:green;", method: :delete %>
304
-
305
- <% else %>
306
-
307
- <%= link_to '登録', user_favorites_path(user_id:current_user.id, nutrition_id:nutrition.id), :style=>"color:green;", method: :post %>
308
-
309
- <% end %>
310
-
311
- <% if current_user.id == nutrition.user.id %>
312
-
313
- <%= link_to '編集', edit_nutrition_path(nutrition.id), :style=>"color:green;", method: :get %>
314
-
315
- <%= link_to '削除', nutrition_path(nutrition.id), :style=>"color:green;", method: :delete, data: { confirm: '削除しますか?'} %>
316
-
317
- <% end %>
318
-
319
- <% end %>
320
-
321
- </li>
322
-
323
- </ul>
324
-
325
- </div>
326
-
327
- <td><%= number_with_precision(nutrition.calorie, precision: 1, strip_insignificant_zeros: true) %></td>
328
-
329
- <td><%= number_with_precision(nutrition.protein, precision: 1, strip_insignificant_zeros: true) %></td>
330
-
331
- <td><%= number_with_precision(nutrition.lipid, precision: 1, strip_insignificant_zeros: true) %></td>
332
-
333
- <td><%= number_with_precision(nutrition.carbohydrate, precision: 1, strip_insignificant_zeros: true) %></td>
334
-
335
- <td><%= number_with_precision(nutrition.potassium, precision: 1, strip_insignificant_zeros: true) %></td>
336
-
337
- <td><%= number_with_precision(nutrition.calcium, precision: 1, strip_insignificant_zeros: true) %></td>
338
-
339
- <td><%= number_with_precision(nutrition.iron, precision: 1, strip_insignificant_zeros: true) %></td>
340
-
341
- <td><%= number_with_precision(nutrition.vitamin_a, precision: 1, strip_insignificant_zeros: true) %></td>
342
-
343
- <td><%= number_with_precision(nutrition.vitamin_b1, precision: 1, strip_insignificant_zeros: true) %></td>
344
-
345
- <td><%= number_with_precision(nutrition.vitamin_b2, precision: 1, strip_insignificant_zeros: true) %></td>
346
-
347
- <td><%= number_with_precision(nutrition.vitamin_c, precision: 1, strip_insignificant_zeros: true) %></td>
348
-
349
- <td><%= number_with_precision(nutrition.dietary_fiber, precision: 1, strip_insignificant_zeros: true) %></td>
350
-
351
- </tr>
352
-
353
- <% end %>
354
-
355
- </table>
356
-
357
- </div>
358
-
359
-
360
-
361
- コード
362
-
363
- ```
364
-
365
-
366
-
367
- また、マイグレーションファイルは以下になります。
368
-
369
- ```class ChangeDataAllNutrition < ActiveRecord::Migration[6.0]
370
-
371
- def up
372
-
373
- change_column :nutritions, :protein, :float
374
-
375
- change_column :nutritions, :lipid, :float
376
-
377
- change_column :nutritions, :carbohydrate, :float
378
-
379
- change_column :nutritions, :potassium, :float
380
-
381
- change_column :nutritions, :calcium, :float
382
-
383
- change_column :nutritions, :iron, :float
384
-
385
- change_column :nutritions, :vitamin_a, :float
386
-
387
- change_column :nutritions, :vitamin_b1, :float
388
-
389
- change_column :nutritions, :vitamin_b2, :float
390
-
391
- change_column :nutritions, :vitamin_c, :float
392
-
393
- change_column :nutritions, :dietary_fiber, :float
394
-
395
- rename_column :nutritions, :dietary_fiber, :dietary_fiber
396
-
397
- end
398
-
399
- end
400
-
401
-
402
-
403
- コード
404
-
405
- ```
406
-
407
-
408
-
409
- お力添頂けますと幸いです。

1

tuki

2020/11/15 11:31

投稿

tarotarotarotar
tarotarotarotar

スコア41

test CHANGED
File without changes
test CHANGED
@@ -364,4 +364,46 @@
364
364
 
365
365
 
366
366
 
367
+ また、マイグレーションファイルは以下になります。
368
+
369
+ ```class ChangeDataAllNutrition < ActiveRecord::Migration[6.0]
370
+
371
+ def up
372
+
373
+ change_column :nutritions, :protein, :float
374
+
375
+ change_column :nutritions, :lipid, :float
376
+
377
+ change_column :nutritions, :carbohydrate, :float
378
+
379
+ change_column :nutritions, :potassium, :float
380
+
381
+ change_column :nutritions, :calcium, :float
382
+
383
+ change_column :nutritions, :iron, :float
384
+
385
+ change_column :nutritions, :vitamin_a, :float
386
+
387
+ change_column :nutritions, :vitamin_b1, :float
388
+
389
+ change_column :nutritions, :vitamin_b2, :float
390
+
391
+ change_column :nutritions, :vitamin_c, :float
392
+
393
+ change_column :nutritions, :dietary_fiber, :float
394
+
395
+ rename_column :nutritions, :dietary_fiber, :dietary_fiber
396
+
397
+ end
398
+
399
+ end
400
+
401
+
402
+
403
+ コード
404
+
405
+ ```
406
+
407
+
408
+
367
409
  お力添頂けますと幸いです。