質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

解決済

2回答

7658閲覧

「rails」Rspecで表示されたエラー内容について

paserin

総合スコア4

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

0クリップ

投稿2021/03/02 03:16

編集2021/03/02 05:19

発生している問題・エラーメッセージ

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

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

maisumakun

2021/03/02 03:20

テストコードも記載してください。
paserin

2021/03/02 04:20

返信が遅れてしまってすみません。 恐らくテストコードだと思われるものを追記しました。 間違っていたらすみません
guest

回答2

0

ベストアンサー

params.permit(:title, :body) ここですね。
params.require(:book).permit(:title, :body)

投稿2021/03/02 06:48

winterboum

総合スコア23347

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

paserin

2021/03/02 08:57

if @book.save flash[:notice] = "Book was successfully created." redirect_to book_path(book.id) if @book.save flash[:notice] = "Book was successfully created." redirect_to book_path(book.id) if @book.save flash[:notice] = "Book was successfully created." redirect_to book_path(@book) 指摘いただいた部分を改善した所、エラーメッセージのエラーが改善され、加えて上記のように(book.id)を(@book)に変更するとエラーが全てなくなりました! ありがとうございました!
paserin

2021/03/02 09:00

指摘部分に加えて、createのedirect_to book_path(book.id)を(@book)に変更することですべてのエラーが解消されました! ありがとうございました!
guest

0

・そもそもローカル環境で投稿でき、successfullyが出るかどうか
→ローカル環境とテスト環境の問題の切り分け

を見たらよさそうな気がします。

ローカル環境でそもそも投稿できないのであれば、テストコードの問題ではなく、普通のコードの方を見たほうが良さそうですね。

投稿2021/03/02 06:09

educ_gt

総合スコア282

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問