発生している問題・エラーメッセージ
3つエラーが出てしまうのですが、完全に行き詰ってしまったので、なぜこのエラーが出ているのか、解決策を教えて頂きたいです。
新規投稿のフォームが空欄だとエラー文が出るように書いているのですが、titleとbodyの両方に入力をしたにもかかわらずエラー文が出力されるようになってしまい、新規投稿が出来ない状態になっています。
Failures: 1) 投稿のテスト 一覧画面のテスト 投稿処理に関するテスト 投稿に成功しサクセスメッセージが表示されるか Failure/Error: expect(page).to have_content 'successfully' expected to find text "successfully" in "Books\nTitle Body hoge body show edit destroy\nNew Book\n2 errors prohibited this book from being saved:\nTitle can't be blank Body can't be blank\n \nTitle\nBody" # ./spec/system/books_spec.rb:60:in `block (4 levels) in <main>' 2) 投稿のテスト 一覧画面のテスト 投稿処理に関するテスト 投稿後のリダイレクト先は正しいか Failure/Error: expect(page).to have_current_path book_path(Book.last) expected "/books" to equal "/books/1" # ./spec/system/books_spec.rb:71:in `block (4 levels) in <main>' 3) 投稿のテスト 編集画面のテスト 更新処理に関するテスト 更新に失敗しエラーメッセージが表示されるか Failure/Error: expect(page).to have_content 'error' expected to find text "error" in "Book was successfully updated.\nTitle: hoge\nBody: body\nEdit Back" # ./spec/system/books_spec.rb:156:in `block (4 levels) in <main>'
該当のソースコード
books_controller.rb
class BooksController < ApplicationController def index @books = Book.all @book = Book.new end def create @book = Book.new(book_params) if @book.save flash[:notice] = "Book was successfully created." redirect_to book_path(book.id) else @books=Book.all render action :index end end def show @book = Book.find(params[:id]) end def edit @book = Book.find(params[:id]) end def update @book = Book.find(params[:id]) if @book.update(book_params) flash[:notice] = "Book was successfully updated." redirect_to book_path(@book) else @books=Book.all render action :edit end end def destroy book = Book.find(params[:id]) book.destroy flash[:notice] = "Book was successfully destroyed." redirect_to books_path end private def book_params params.permit(:title, :body) end end
index.html.erb
<h1>Books</h1> <table> <tr> <th> Title </th> <th>Body</th> </tr> <% @books.each do |book| %> <tr> <th> <%= book.title %> </th> <th> <%= book.body %> </th> <th> <%= link_to "show", book_path(book.id) %> </th> <th> <%= link_to "edit", edit_book_path(book.id) %> </th> <th> <%= link_to "destroy", book_path(book.id) , method: :delete %> </th> </tr> <%end%> </table> <h1>New Book</h1> <%= form_with model:@book, local:true do |f| %> <% if @book.errors.any? %> <div id="error_explanation"> <h2><%= pluralize(@book.errors.count, "error") %> prohibited this book from being saved:</h2> <ul> <% @book.errors.full_messages.each do |message| %> <li><%= message %></li> <% end %> </ul> </div> <% end %> <h4>Title</h4> <%= f.text_field :title %> <h4>Body</h4> <%= f.text_area :body %> <%= f.submit 'Create Book' %> <% end %>
edit.html.erb
<h1>Editing Book</h1> <%= form_with model:@book, local:true do |f| %> <% if @book.errors.any? %> <div id="error_explanation"> <h2><%= pluralize(@book.errors.count, "error") %> prohibited this book from being saved:</h2> <ul> <% @book.errors.full_messages.each do |message| %> <li><%= message %></li> <% end %> </ul> </div> <% end %> <h4>Title</h4> <%= f.text_field :title %> <h4>Body</h4> <%= f.text_area :body %> <%= f.submit 'Update Book' %> <% end %> <%= link_to "show", book_path(@book) %> <%= link_to "Back", books_path %>
show.html.erb
<p>Title: <%= @book.title %></p> <p>Body: <%= @book.body%></p> <%= link_to "Edit", edit_book_path(@book) %> <%= link_to "Back", books_path %>
routes.rb
Rails.application.routes.draw do # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html resources :books root to: 'homes#top' end
テストコード
require 'rails_helper' describe '投稿のテスト' do let!(:book) { create(:book,title:'hoge',body:'body') } describe 'トップ画面(root_path)のテスト' do before do visit root_path end context '表示の確認' do it 'トップ画面(root_path)に一覧ページへのリンクが表示されているか' do expect(page).to have_link "", href: books_path end it 'root_pathが"/"であるか' do expect(current_path).to eq('/') end end end describe "一覧画面のテスト" do before do visit books_path end context '一覧の表示とリンクの確認' do it "bookの一覧表示(tableタグ)と投稿フォームが同一画面に表示されているか" do expect(page).to have_selector 'table' expect(page).to have_field 'book[title]' expect(page).to have_field 'book[body]' end it "bookのタイトルと感想を表示し、詳細・編集・削除のリンクが表示されているか" do (1..5).each do |i| Book.create(title:'hoge'+i.to_s,body:'body'+i.to_s) end visit books_path Book.all.each_with_index do |book,i| j = i * 3 expect(page).to have_content book.title expect(page).to have_content book.body # Showリンク show_link = find_all('a')[j] expect(show_link.native.inner_text).to match(/show/i) expect(show_link[:href]).to eq book_path(book) # Editリンク show_link = find_all('a')[j+1] expect(show_link.native.inner_text).to match(/edit/i) expect(show_link[:href]).to eq edit_book_path(book) # Destroyリンク show_link = find_all('a')[j+2] expect(show_link.native.inner_text).to match(/destroy/i) expect(show_link[:href]).to eq book_path(book) end end it 'Create Bookボタンが表示される' do expect(page).to have_button 'Create Book' end end context '投稿処理に関するテスト' do it '投稿に成功しサクセスメッセージが表示されるか' do fill_in 'book[title]', with: Faker::Lorem.characters(number:5) fill_in 'book[body]', with: Faker::Lorem.characters(number:20) click_button 'Create Book' expect(page).to have_content 'successfully' end it '投稿に失敗する' do click_button 'Create Book' expect(page).to have_content 'error' expect(current_path).to eq('/books') end it '投稿後のリダイレクト先は正しいか' do fill_in 'book[title]', with: Faker::Lorem.characters(number:5) fill_in 'book[body]', with: Faker::Lorem.characters(number:20) click_button 'Create Book' expect(page).to have_current_path book_path(Book.last) end end context 'book削除のテスト' do it 'bookの削除' do expect{ book.destroy }.to change{ Book.count }.by(-1) # ※本来はダイアログのテストまで行うがココではデータが削除されることだけをテスト end end end describe '詳細画面のテスト' do before do visit book_path(book) end context '表示の確認' do it '本のタイトルと感想が画面に表示されていること' do expect(page).to have_content book.title expect(page).to have_content book.body end it 'Editリンクが表示される' do edit_link = find_all('a')[0] expect(edit_link.native.inner_text).to match(/edit/i) end it 'Backリンクが表示される' do back_link = find_all('a')[1] expect(back_link.native.inner_text).to match(/back/i) end end context 'リンクの遷移先の確認' do it 'Editの遷移先は編集画面か' do edit_link = find_all('a')[0] edit_link.click expect(current_path).to eq('/books/' + book.id.to_s + '/edit') end it 'Backの遷移先は一覧画面か' do back_link = find_all('a')[1] back_link.click expect(page).to have_current_path books_path end end end describe '編集画面のテスト' do before do visit edit_book_path(book) end context '表示の確認' do it '編集前のタイトルと感想がフォームに表示(セット)されている' do expect(page).to have_field 'book[title]', with: book.title expect(page).to have_field 'book[body]', with: book.body end it 'Update Bookボタンが表示される' do expect(page).to have_button 'Update Book' end it 'Showリンクが表示される' do show_link = find_all('a')[0] expect(show_link.native.inner_text).to match(/show/i) end it 'Backリンクが表示される' do back_link = find_all('a')[1] expect(back_link.native.inner_text).to match(/back/i) end end context 'リンクの遷移先の確認' do it 'Showの遷移先は詳細画面か' do show_link = find_all('a')[0] show_link.click expect(current_path).to eq('/books/' + book.id.to_s) end it 'Backの遷移先は一覧画面か' do back_link = find_all('a')[1] back_link.click expect(page).to have_current_path books_path end end context '更新処理に関するテスト' do it '更新に成功しサクセスメッセージが表示されるか' do fill_in 'book[title]', with: Faker::Lorem.characters(number:5) fill_in 'book[body]', with: Faker::Lorem.characters(number:20) click_button 'Update Book' expect(page).to have_content 'successfully' end it '更新に失敗しエラーメッセージが表示されるか' do fill_in 'book[title]', with: "" fill_in 'book[body]', with: "" click_button 'Update Book' expect(page).to have_content 'error' end it '更新後のリダイレクト先は正しいか' do fill_in 'book[title]', with: Faker::Lorem.characters(number:5) fill_in 'book[body]', with: Faker::Lorem.characters(number:20) click_button 'Update Book' expect(page).to have_current_path book_path(book) end end end end
回答2件
あなたの回答
tips
プレビュー