質問編集履歴

3

問題の追加

2021/03/02 05:19

投稿

paserin
paserin

スコア4

test CHANGED
File without changes
test CHANGED
@@ -4,6 +4,8 @@
4
4
 
5
5
  3つエラーが出てしまうのですが、完全に行き詰ってしまったので、なぜこのエラーが出ているのか、解決策を教えて頂きたいです。
6
6
 
7
+ 新規投稿のフォームが空欄だとエラー文が出るように書いているのですが、titleとbodyの両方に入力をしたにもかかわらずエラー文が出力されるようになってしまい、新規投稿が出来ない状態になっています。
8
+
7
9
  ```
8
10
 
9
11
 

2

テストコードを追記しました

2021/03/02 05:19

投稿

paserin
paserin

スコア4

test CHANGED
@@ -1 +1 @@
1
- rails」Rspecで表示されたエラー内容について
1
+ rails」Rspecで表示されたエラー内容について
test CHANGED
File without changes

1

テストコードを追記しました 文字制限があったので多少省きました。

2021/03/02 05:08

投稿

paserin
paserin

スコア4

test CHANGED
File without changes
test CHANGED
@@ -1,142 +1,10 @@
1
- ### 前提・実現したいこと
2
-
3
-
4
-
5
- railsで以下の要件に沿ったアプリケーションを作っています。
6
-
7
-
8
-
9
- ・本の感想投稿へのCRUD機能
10
-
11
- ・感想投稿の際、本のタイトルと感想に入力がなされていない場合、登録不可とする(validationの設定)
12
-
13
- ・scaffoldの使用禁止。
14
-
15
- ・localhost:3000/のURLを叩いた時にTOP画面が表示される(ルーティングの設定)
16
-
17
- ・テーブル名は"posts",カラムは"title","body"でモデルを作成
18
-
19
- ・ストロングパラメータを用いる
20
-
21
- ・新規投稿または投稿の編集を行うと投稿した詳細画面(show.html.erb)に飛ぶ
22
-
23
-
24
-
25
- 【レイアウト】
26
-
27
- ・新規投稿フォームと投稿一覧表示を同一のページ内に置く
28
-
29
-
30
-
31
- 【デザイン】
32
-
33
- ・一覧部分にtableを用いる
34
-
35
-
36
-
37
1
  ### 発生している問題・エラーメッセージ
38
2
 
39
3
 
40
4
 
41
- 3つエラーが出てしまうのですが、完全に行き詰ってしまったので解決策を教えて頂きたいです。
5
+ 3つエラーが出てしまうのですが、完全に行き詰ってしまったので、なぜこのエラーが出ているのか、解決策を教えて頂きたいです。
42
-
6
+
43
- ```
7
+ ```
44
-
45
- Book モデルに関するテスト
46
-
47
- 実際に保存してみる
48
-
49
- 有効な投稿内容の場合は保存されるか
50
-
51
- 空白のバリデーションチェック
52
-
53
- titleが空白の場合にバリデーションチェックされ空白のエラーメッセージが返ってきているか
54
-
55
- bodyが空白の場合にバリデーションチェックされ空白のエラーメッセージが返ってきているか
56
-
57
- titleを空白で投稿した場合に画面にエラーメッセージが表示されているか
58
-
59
- エラーメッセージは正しく表示されるか
60
-
61
- bodyを空白で投稿した場合に画面にエラーメッセージが表示されているか
62
-
63
- エラーメッセージは正しく表示されるか
64
-
65
-
66
-
67
- 投稿のテスト
68
-
69
- トップ画面(root_path)のテスト
70
-
71
- 表示の確認
72
-
73
- トップ画面(root_path)に一覧ページへのリンクが表示されているか
74
-
75
- root_pathが"/"であるか
76
-
77
- 一覧画面のテスト
78
-
79
- 一覧の表示とリンクの確認
80
-
81
- bookの一覧表示(tableタグ)と投稿フォームが同一画面に表示されているか
82
-
83
- bookのタイトルと感想を表示し、詳細・編集・削除のリンクが表示されているか
84
-
85
- Create Bookボタンが表示される
86
-
87
- 投稿処理に関するテスト
88
-
89
- 投稿に成功しサクセスメッセージが表示されるか (FAILED - 1)
90
-
91
- 投稿に失敗する
92
-
93
- 投稿後のリダイレクト先は正しいか (FAILED - 2)
94
-
95
- book削除のテスト
96
-
97
- bookの削除
98
-
99
- 詳細画面のテスト
100
-
101
- 表示の確認
102
-
103
- 本のタイトルと感想が画面に表示されていること
104
-
105
- Editリンクが表示される
106
-
107
- Backリンクが表示される
108
-
109
- リンクの遷移先の確認
110
-
111
- Editの遷移先は編集画面か
112
-
113
- Backの遷移先は一覧画面か
114
-
115
- 編集画面のテスト
116
-
117
- 表示の確認
118
-
119
- 編集前のタイトルと感想がフォームに表示(セット)されている
120
-
121
- Update Bookボタンが表示される
122
-
123
- Showリンクが表示される
124
-
125
- Backリンクが表示される
126
-
127
- リンクの遷移先の確認
128
-
129
- Showの遷移先は詳細画面か
130
-
131
- Backの遷移先は一覧画面か
132
-
133
- 更新処理に関するテスト
134
-
135
- 更新に成功しサクセスメッセージが表示されるか
136
-
137
- 更新に失敗しエラーメッセージが表示されるか (FAILED - 3)
138
-
139
- 更新後のリダイレクト先は正しいか
140
8
 
141
9
 
142
10
 
@@ -174,24 +42,6 @@
174
42
 
175
43
 
176
44
 
177
- Finished in 0.76962 seconds (files took 0.98823 seconds to load)
178
-
179
- 28 examples, 3 failures
180
-
181
-
182
-
183
- Failed examples:
184
-
185
-
186
-
187
- rspec ./spec/system/books_spec.rb:56 # 投稿のテスト 一覧画面のテスト 投稿処理に関するテスト 投稿に成功しサクセスメッセージが表示されるか
188
-
189
- rspec ./spec/system/books_spec.rb:67 # 投稿のテスト 一覧画面のテスト 投稿処理に関するテスト 投稿後のリダイレクト先は正しいか
190
-
191
- rspec ./spec/system/books_spec.rb:152 # 投稿のテスト 編集画面のテスト 更新処理に関するテスト 更新に失敗しエラーメッセージが表示されるか
192
-
193
-
194
-
195
45
  ```
196
46
 
197
47
 
@@ -200,396 +50,648 @@
200
50
 
201
51
 
202
52
 
53
+
54
+
203
- homes_controller.rb
55
+ books_controller.rb
204
-
56
+
205
- ```
57
+ ```
206
-
58
+
207
- class HomesController < ApplicationController
59
+ class BooksController < ApplicationController
208
-
209
-
210
-
60
+
61
+
62
+
211
- def top
63
+ def index
64
+
212
-
65
+ @books = Book.all
66
+
67
+ @book = Book.new
68
+
213
- end
69
+ end
70
+
71
+
72
+
73
+ def create
74
+
75
+ @book = Book.new(book_params)
76
+
77
+ if @book.save
78
+
79
+ flash[:notice] = "Book was successfully created."
80
+
81
+ redirect_to book_path(book.id)
82
+
83
+ else
84
+
85
+ @books=Book.all
86
+
87
+ render action :index
88
+
89
+ end
90
+
91
+ end
92
+
93
+
94
+
95
+ def show
96
+
97
+ @book = Book.find(params[:id])
98
+
99
+ end
100
+
101
+
102
+
103
+ def edit
104
+
105
+ @book = Book.find(params[:id])
106
+
107
+ end
108
+
109
+
110
+
111
+ def update
112
+
113
+ @book = Book.find(params[:id])
114
+
115
+ if @book.update(book_params)
116
+
117
+ flash[:notice] = "Book was successfully updated."
118
+
119
+ redirect_to book_path(@book)
120
+
121
+ else
122
+
123
+ @books=Book.all
124
+
125
+ render action :edit
126
+
127
+ end
128
+
129
+ end
130
+
131
+
132
+
133
+ def destroy
134
+
135
+ book = Book.find(params[:id])
136
+
137
+ book.destroy
138
+
139
+ flash[:notice] = "Book was successfully destroyed."
140
+
141
+ redirect_to books_path
142
+
143
+ end
144
+
145
+
146
+
147
+ private
148
+
149
+ def book_params
150
+
151
+ params.permit(:title, :body)
152
+
153
+ end
154
+
155
+
214
156
 
215
157
  end
216
158
 
217
159
  ```
218
160
 
219
- books_controller.rb
220
-
221
- ```
222
-
223
- class BooksController < ApplicationController
224
-
225
-
226
-
227
- def index
228
-
229
- @books = Book.all
230
-
231
- @book = Book.new
232
-
233
- end
234
-
235
-
236
-
237
- def create
238
-
239
- @book = Book.new(book_params)
240
-
241
- if @book.save
242
-
243
- flash[:notice] = "Book was successfully created."
244
-
245
- redirect_to book_path(book.id)
246
-
247
- else
248
-
249
- @books=Book.all
250
-
251
- render action :index
252
-
253
- end
254
-
255
- end
256
-
257
-
258
-
259
- def show
260
-
261
- @book = Book.find(params[:id])
262
-
263
- end
264
-
265
-
266
-
267
- def edit
268
-
269
- @book = Book.find(params[:id])
270
-
271
- end
272
-
273
-
274
-
275
- def update
276
-
277
- @book = Book.find(params[:id])
278
-
279
- if @book.update(book_params)
280
-
281
- flash[:notice] = "Book was successfully updated."
282
-
283
- redirect_to book_path(@book)
284
-
285
- else
286
-
287
- @books=Book.all
288
-
289
- render action :edit
290
-
291
- end
292
-
293
- end
294
-
295
-
296
-
297
- def destroy
298
-
299
- book = Book.find(params[:id])
300
-
301
- book.destroy
302
-
303
- flash[:notice] = "Book was successfully destroyed."
304
-
305
- redirect_to books_path
306
-
307
- end
308
-
309
-
310
-
311
- private
312
-
313
- def book_params
314
-
315
- params.permit(:title, :body)
316
-
317
- end
318
-
319
-
161
+ index.html.erb
162
+
163
+ ```
164
+
165
+ <h1>Books</h1>
166
+
167
+
168
+
169
+ <table>
170
+
171
+ <tr>
172
+
173
+ <th>
174
+
175
+ Title
176
+
177
+ </th>
178
+
179
+ <th>Body</th>
180
+
181
+ </tr>
182
+
183
+ <% @books.each do |book| %>
184
+
185
+ <tr>
186
+
187
+ <th>
188
+
189
+ <%= book.title %>
190
+
191
+ </th>
192
+
193
+ <th>
194
+
195
+ <%= book.body %>
196
+
197
+ </th>
198
+
199
+ <th>
200
+
201
+ <%= link_to "show", book_path(book.id) %>
202
+
203
+ </th>
204
+
205
+ <th>
206
+
207
+ <%= link_to "edit", edit_book_path(book.id) %>
208
+
209
+ </th>
210
+
211
+ <th>
212
+
213
+ <%= link_to "destroy", book_path(book.id) , method: :delete %>
214
+
215
+ </th>
216
+
217
+ </tr>
218
+
219
+ <%end%>
220
+
221
+ </table>
222
+
223
+
224
+
225
+ <h1>New Book</h1>
226
+
227
+
228
+
229
+ <%= form_with model:@book, local:true do |f| %>
230
+
231
+
232
+
233
+ <% if @book.errors.any? %>
234
+
235
+ <div id="error_explanation">
236
+
237
+ <h2><%= pluralize(@book.errors.count, "error") %> prohibited this book from being saved:</h2>
238
+
239
+ <ul>
240
+
241
+ <% @book.errors.full_messages.each do |message| %>
242
+
243
+ <li><%= message %></li>
244
+
245
+ <% end %>
246
+
247
+ </ul>
248
+
249
+ </div>
250
+
251
+  <% end %>
252
+
253
+
254
+
255
+ <h4>Title</h4>
256
+
257
+ <%= f.text_field :title %>
258
+
259
+
260
+
261
+ <h4>Body</h4>
262
+
263
+ <%= f.text_area :body %>
264
+
265
+
266
+
267
+ <%= f.submit 'Create Book' %>
268
+
269
+
270
+
271
+ <% end %>
272
+
273
+ ```
274
+
275
+ edit.html.erb
276
+
277
+ ```
278
+
279
+ <h1>Editing Book</h1>
280
+
281
+
282
+
283
+ <%= form_with model:@book, local:true do |f| %>
284
+
285
+
286
+
287
+ <% if @book.errors.any? %>
288
+
289
+ <div id="error_explanation">
290
+
291
+ <h2><%= pluralize(@book.errors.count, "error") %> prohibited this book from being saved:</h2>
292
+
293
+ <ul>
294
+
295
+ <% @book.errors.full_messages.each do |message| %>
296
+
297
+ <li><%= message %></li>
298
+
299
+ <% end %>
300
+
301
+ </ul>
302
+
303
+ </div>
304
+
305
+  <% end %>
306
+
307
+
308
+
309
+ <h4>Title</h4>
310
+
311
+ <%= f.text_field :title %>
312
+
313
+ <h4>Body</h4>
314
+
315
+ <%= f.text_area :body %>
316
+
317
+ <%= f.submit 'Update Book' %>
318
+
319
+ <% end %>
320
+
321
+ <%= link_to "show", book_path(@book) %>
322
+
323
+ <%= link_to "Back", books_path %>
324
+
325
+ ```
326
+
327
+ show.html.erb
328
+
329
+ ```
330
+
331
+ <p>Title: <%= @book.title %></p>
332
+
333
+ <p>Body: <%= @book.body%></p>
334
+
335
+ <%= link_to "Edit", edit_book_path(@book) %>
336
+
337
+ <%= link_to "Back", books_path %>
338
+
339
+ ```
340
+
341
+
342
+
343
+ routes.rb
344
+
345
+ ```
346
+
347
+ Rails.application.routes.draw do
348
+
349
+ # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
350
+
351
+ resources :books
352
+
353
+ root to: 'homes#top'
320
354
 
321
355
  end
322
356
 
323
357
  ```
324
358
 
325
- index.html.erb
326
-
327
- ```
328
-
329
- <h1>Books</h1>
330
-
331
-
332
-
333
- <table>
334
-
335
- <tr>
336
-
337
- <th>
338
-
339
- Title
340
-
341
- </th>
342
-
343
- <th>Body</th>
344
-
345
- </tr>
346
-
347
- <% @books.each do |book| %>
348
-
349
- <tr>
350
-
351
- <th>
352
-
353
- <%= book.title %>
354
-
355
- </th>
356
-
357
- <th>
358
-
359
- <%= book.body %>
360
-
361
- </th>
362
-
363
- <th>
364
-
365
- <%= link_to "show", book_path(book.id) %>
366
-
367
- </th>
368
-
369
- <th>
370
-
371
- <%= link_to "edit", edit_book_path(book.id) %>
372
-
373
- </th>
374
-
375
- <th>
376
-
377
- <%= link_to "destroy", book_path(book.id) , method: :delete %>
378
-
379
- </th>
380
-
381
- </tr>
382
-
383
- <%end%>
384
-
385
- </table>
386
-
387
-
388
-
389
- <h1>New Book</h1>
390
-
391
-
392
-
393
- <%= form_with model:@book, local:true do |f| %>
394
-
395
-
396
-
397
- <% if @book.errors.any? %>
398
-
399
- <div id="error_explanation">
400
-
401
- <h2><%= pluralize(@book.errors.count, "error") %> prohibited this book from being saved:</h2>
402
-
403
- <ul>
404
-
405
- <% @book.errors.full_messages.each do |message| %>
406
-
407
- <li><%= message %></li>
408
-
409
- <% end %>
410
-
411
- </ul>
412
-
413
- </div>
414
-
415
-  <% end %>
416
-
417
-
418
-
419
- <h4>Title</h4>
420
-
421
- <%= f.text_field :title %>
422
-
423
-
424
-
425
- <h4>Body</h4>
426
-
427
- <%= f.text_area :body %>
428
-
429
-
430
-
431
- <%= f.submit 'Create Book' %>
432
-
433
-
434
-
435
- <% end %>
436
-
437
- ```
438
-
439
- edit.html.erb
440
-
441
- ```
442
-
443
- <h1>Editing Book</h1>
444
-
445
-
446
-
447
- <%= form_with model:@book, local:true do |f| %>
448
-
449
-
450
-
451
- <% if @book.errors.any? %>
452
-
453
- <div id="error_explanation">
454
-
455
- <h2><%= pluralize(@book.errors.count, "error") %> prohibited this book from being saved:</h2>
456
-
457
- <ul>
458
-
459
- <% @book.errors.full_messages.each do |message| %>
460
-
461
- <li><%= message %></li>
462
-
463
- <% end %>
464
-
465
- </ul>
466
-
467
- </div>
468
-
469
-  <% end %>
470
-
471
-
472
-
473
- <h4>Title</h4>
474
-
475
- <%= f.text_field :title %>
476
-
477
- <h4>Body</h4>
478
-
479
- <%= f.text_area :body %>
480
-
481
- <%= f.submit 'Update Book' %>
482
-
483
- <% end %>
484
-
485
- <%= link_to "show", book_path(@book) %>
486
-
487
- <%= link_to "Back", books_path %>
488
-
489
- ```
490
-
491
- index.html.erb
492
-
493
- ```
494
-
495
- <p>Title: <%= @book.title %></p>
496
-
497
- <p>Body: <%= @book.body%></p>
498
-
499
- <%= link_to "Edit", edit_book_path(@book) %>
500
-
501
- <%= link_to "Back", books_path %>
502
-
503
- ```
504
-
505
- index.html.erb
506
-
507
- ```
508
-
509
- <h1>ようこそBookersへ!</h1>
510
-
511
- <p>Bookers では、さまざまな書籍に関するあなたの意見や</p>
512
-
513
- <p>印象を共有し交換することができます</p>
514
-
515
- <p><%= link_to "start", books_path %></p>
516
-
517
- ```
518
-
519
- routes.rb
520
-
521
- ```
522
-
523
- Rails.application.routes.draw do
524
-
525
- # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
526
-
527
- resources :books
528
-
529
- root to: 'homes#top'
359
+
360
+
361
+ テストコード
362
+
363
+ ```
364
+
365
+ require 'rails_helper'
366
+
367
+
368
+
369
+ describe '投稿のテスト' do
370
+
371
+ let!(:book) { create(:book,title:'hoge',body:'body') }
372
+
373
+ describe 'トップ画面(root_path)のテスト' do
374
+
375
+ before do
376
+
377
+ visit root_path
378
+
379
+ end
380
+
381
+ context '表示の確認' do
382
+
383
+ it 'トップ画面(root_path)に一覧ページへのリンクが表示されているか' do
384
+
385
+ expect(page).to have_link "", href: books_path
386
+
387
+ end
388
+
389
+ it 'root_pathが"/"であるか' do
390
+
391
+ expect(current_path).to eq('/')
392
+
393
+ end
394
+
395
+ end
396
+
397
+ end
398
+
399
+ describe "一覧画面のテスト" do
400
+
401
+ before do
402
+
403
+ visit books_path
404
+
405
+ end
406
+
407
+ context '一覧の表示とリンクの確認' do
408
+
409
+ it "bookの一覧表示(tableタグ)と投稿フォームが同一画面に表示されているか" do
410
+
411
+ expect(page).to have_selector 'table'
412
+
413
+ expect(page).to have_field 'book[title]'
414
+
415
+ expect(page).to have_field 'book[body]'
416
+
417
+ end
418
+
419
+ it "bookのタイトルと感想を表示し、詳細・編集・削除のリンクが表示されているか" do
420
+
421
+ (1..5).each do |i|
422
+
423
+ Book.create(title:'hoge'+i.to_s,body:'body'+i.to_s)
424
+
425
+ end
426
+
427
+ visit books_path
428
+
429
+ Book.all.each_with_index do |book,i|
430
+
431
+ j = i * 3
432
+
433
+ expect(page).to have_content book.title
434
+
435
+ expect(page).to have_content book.body
436
+
437
+ # Showリンク
438
+
439
+ show_link = find_all('a')[j]
440
+
441
+ expect(show_link.native.inner_text).to match(/show/i)
442
+
443
+ expect(show_link[:href]).to eq book_path(book)
444
+
445
+ # Editリンク
446
+
447
+ show_link = find_all('a')[j+1]
448
+
449
+ expect(show_link.native.inner_text).to match(/edit/i)
450
+
451
+ expect(show_link[:href]).to eq edit_book_path(book)
452
+
453
+ # Destroyリンク
454
+
455
+ show_link = find_all('a')[j+2]
456
+
457
+ expect(show_link.native.inner_text).to match(/destroy/i)
458
+
459
+ expect(show_link[:href]).to eq book_path(book)
460
+
461
+ end
462
+
463
+ end
464
+
465
+ it 'Create Bookボタンが表示される' do
466
+
467
+ expect(page).to have_button 'Create Book'
468
+
469
+ end
470
+
471
+ end
472
+
473
+ context '投稿処理に関するテスト' do
474
+
475
+ it '投稿に成功しサクセスメッセージが表示されるか' do
476
+
477
+ fill_in 'book[title]', with: Faker::Lorem.characters(number:5)
478
+
479
+ fill_in 'book[body]', with: Faker::Lorem.characters(number:20)
480
+
481
+ click_button 'Create Book'
482
+
483
+ expect(page).to have_content 'successfully'
484
+
485
+ end
486
+
487
+ it '投稿に失敗する' do
488
+
489
+ click_button 'Create Book'
490
+
491
+ expect(page).to have_content 'error'
492
+
493
+ expect(current_path).to eq('/books')
494
+
495
+ end
496
+
497
+ it '投稿後のリダイレクト先は正しいか' do
498
+
499
+ fill_in 'book[title]', with: Faker::Lorem.characters(number:5)
500
+
501
+ fill_in 'book[body]', with: Faker::Lorem.characters(number:20)
502
+
503
+ click_button 'Create Book'
504
+
505
+ expect(page).to have_current_path book_path(Book.last)
506
+
507
+ end
508
+
509
+ end
510
+
511
+ context 'book削除のテスト' do
512
+
513
+ it 'bookの削除' do
514
+
515
+ expect{ book.destroy }.to change{ Book.count }.by(-1)
516
+
517
+ # ※本来はダイアログのテストまで行うがココではデータが削除されることだけをテスト
518
+
519
+ end
520
+
521
+ end
522
+
523
+ end
524
+
525
+ describe '詳細画面のテスト' do
526
+
527
+ before do
528
+
529
+ visit book_path(book)
530
+
531
+ end
532
+
533
+ context '表示の確認' do
534
+
535
+ it '本のタイトルと感想が画面に表示されていること' do
536
+
537
+ expect(page).to have_content book.title
538
+
539
+ expect(page).to have_content book.body
540
+
541
+ end
542
+
543
+ it 'Editリンクが表示される' do
544
+
545
+ edit_link = find_all('a')[0]
546
+
547
+ expect(edit_link.native.inner_text).to match(/edit/i)
548
+
549
+ end
550
+
551
+ it 'Backリンクが表示される' do
552
+
553
+ back_link = find_all('a')[1]
554
+
555
+ expect(back_link.native.inner_text).to match(/back/i)
556
+
557
+ end
558
+
559
+ end
560
+
561
+ context 'リンクの遷移先の確認' do
562
+
563
+ it 'Editの遷移先は編集画面か' do
564
+
565
+ edit_link = find_all('a')[0]
566
+
567
+ edit_link.click
568
+
569
+ expect(current_path).to eq('/books/' + book.id.to_s + '/edit')
570
+
571
+ end
572
+
573
+ it 'Backの遷移先は一覧画面か' do
574
+
575
+ back_link = find_all('a')[1]
576
+
577
+ back_link.click
578
+
579
+ expect(page).to have_current_path books_path
580
+
581
+ end
582
+
583
+ end
584
+
585
+ end
586
+
587
+ describe '編集画面のテスト' do
588
+
589
+ before do
590
+
591
+ visit edit_book_path(book)
592
+
593
+ end
594
+
595
+ context '表示の確認' do
596
+
597
+ it '編集前のタイトルと感想がフォームに表示(セット)されている' do
598
+
599
+ expect(page).to have_field 'book[title]', with: book.title
600
+
601
+ expect(page).to have_field 'book[body]', with: book.body
602
+
603
+ end
604
+
605
+ it 'Update Bookボタンが表示される' do
606
+
607
+ expect(page).to have_button 'Update Book'
608
+
609
+ end
610
+
611
+ it 'Showリンクが表示される' do
612
+
613
+ show_link = find_all('a')[0]
614
+
615
+ expect(show_link.native.inner_text).to match(/show/i)
616
+
617
+ end
618
+
619
+ it 'Backリンクが表示される' do
620
+
621
+ back_link = find_all('a')[1]
622
+
623
+ expect(back_link.native.inner_text).to match(/back/i)
624
+
625
+ end
626
+
627
+ end
628
+
629
+ context 'リンクの遷移先の確認' do
630
+
631
+ it 'Showの遷移先は詳細画面か' do
632
+
633
+ show_link = find_all('a')[0]
634
+
635
+ show_link.click
636
+
637
+ expect(current_path).to eq('/books/' + book.id.to_s)
638
+
639
+ end
640
+
641
+ it 'Backの遷移先は一覧画面か' do
642
+
643
+ back_link = find_all('a')[1]
644
+
645
+ back_link.click
646
+
647
+ expect(page).to have_current_path books_path
648
+
649
+ end
650
+
651
+ end
652
+
653
+ context '更新処理に関するテスト' do
654
+
655
+ it '更新に成功しサクセスメッセージが表示されるか' do
656
+
657
+ fill_in 'book[title]', with: Faker::Lorem.characters(number:5)
658
+
659
+ fill_in 'book[body]', with: Faker::Lorem.characters(number:20)
660
+
661
+ click_button 'Update Book'
662
+
663
+ expect(page).to have_content 'successfully'
664
+
665
+ end
666
+
667
+ it '更新に失敗しエラーメッセージが表示されるか' do
668
+
669
+ fill_in 'book[title]', with: ""
670
+
671
+ fill_in 'book[body]', with: ""
672
+
673
+ click_button 'Update Book'
674
+
675
+ expect(page).to have_content 'error'
676
+
677
+ end
678
+
679
+ it '更新後のリダイレクト先は正しいか' do
680
+
681
+ fill_in 'book[title]', with: Faker::Lorem.characters(number:5)
682
+
683
+ fill_in 'book[body]', with: Faker::Lorem.characters(number:20)
684
+
685
+ click_button 'Update Book'
686
+
687
+ expect(page).to have_current_path book_path(book)
688
+
689
+ end
690
+
691
+ end
692
+
693
+ end
530
694
 
531
695
  end
532
696
 
533
697
  ```
534
-
535
- application.html.erb
536
-
537
- ```
538
-
539
- <!DOCTYPE html>
540
-
541
- <html>
542
-
543
- <head>
544
-
545
- <title>BookerApp</title>
546
-
547
- <%= csrf_meta_tags %>
548
-
549
- <%= csp_meta_tag %>
550
-
551
-
552
-
553
- <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
554
-
555
- <%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
556
-
557
- </head>
558
-
559
-
560
-
561
- <body>
562
-
563
- <% if flash[:notice] %>
564
-
565
- <p id="notice">
566
-
567
- <%= flash[:notice] %>
568
-
569
- </p>
570
-
571
- <% end %>
572
-
573
- <%= yield %>
574
-
575
- </body>
576
-
577
- </html>
578
-
579
- ```
580
-
581
- book.rb
582
-
583
- ```
584
-
585
- class Book < ApplicationRecord
586
-
587
- validates :title, presence: true
588
-
589
- validates :body, presence: true
590
-
591
- end
592
-
593
-
594
-
595
- ```