前提・実現したいこと
rspec requestでコントローラー単体テストを実装しています。
deviseを有効にし、@userを生成、ログインした後
@class_communicationを生成しましたが、response.bodyに表示されません。
発生している問題・エラーメッセージ
1) ClassCommunicationsController GET #index indexアクションにリクエストするとレスポンスに学級通信のタイトルが存在する Failure/Error: expect(response.body).to include(@class_communication.title) expected "<!DOCTYPE html>\n<html>\n <head>\n <title>ContactBookApp</title>\n \n \n <meta name=\"v...ass=\"top-tag\"><a href=\"#\">楽ちょ利用規約</a></li>\n </ul>\n</div>\n</footer>\n\n\n </body>\n</html>\n" to include "擬装牛乳ほにゅうびんあくれい。" Diff: @@ -1,64 +1,127 @@ -擬装牛乳ほにゅうびんあくれい。 +<!DOCTYPE html> +<html> + <head> + <title>ContactBookApp</title> + + + <meta name="viewport" content="width=device-width, initial-scale=1.0" /> + <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.13.0/css/all.css" integrity="sha384-Bfad6CLCknfcloXFOyFnlgtENryhrpZCe29RTifKEixXQZ38WheV+i/6YWSzkz3V" crossorigin="anonymous"> + <script type="text/javascript" src="https://js.pay.jp/v1/"></script> + <link rel="stylesheet" media="all" href="/assets/application-32e7dd75cf1608bf44cf6bb71568b2f73f85d2389a49a2f1a51a2f5a27e6453e.css" /> + <script src="/packs-test/js/application-5454c5eab9ccc7f35edf.js"></script> + </head> + + <body> + <header> + + <div class="header-left"> + <i class="fas fa-book-reader"></i> <a href="/">楽ちょ〜電子連絡帳〜</a> + </div> + <div class="header-right"> + <ul class="header-list"> + <li><a href="/users/568">Phuong Hermanさん</a></li> + <li class="top-tag"><a rel="nofollow" data-method="delete" href="/users/sign_out">ログアウト</a></li> + </ul> + </div> +</header> + + <div class="class-communication-main"> + + <div class="class-communication-left"> + + <div class="left-class-communication-title"> + <p>これまでの学級通信</p> + </div> + + <div class="left-class-communication-index"> + </div> + </div> + + <div class="class-communication-right"> + <div class="search-index"> + <form class="search-form" action="/class_communications/search" accept-charset="UTF-8" method="get"> + <input placeholder="学級通信を検索する" class="search-input" type="text" name="keyword" id="keyword" /> + <input type="submit" name="commit" value="検索" class="search-btn" data-disable-with="検索" /> +</form> </div> + </div> +</div>
該当のソースコード
factorybot
1FactoryBot.define do 2 factory :user do 3 nickname { Faker::Name.name } 4 email { Faker::Internet.free_email } 5 password = 'a1' + Faker::Internet.password(min_length: 6, mix_case: false) 6 password { password } 7 password_confirmation { password } 8 introduction { Faker::Lorem.sentence } 9 birthday { Faker::Date.backward } 10 grade_id { Faker::Number.between(from: 2, to: 7) } 11 classroom_id { Faker::Number.between(from: 2, to: 31) } 12 number_id { Faker::Number.between(from: 2, to: 51) } 13 transient do 14 person { Gimei.name } 15 end 16 last_name { person.last.kanji } 17 first_name { person.first.kanji } 18 last_name_reading { person.last.katakana } 19 first_name_reading { person.first.katakana } 20 21 after(:build) do |message| 22 message.image.attach(io: File.open('public/images/output-image1.png'), filename: 'test_image.png') 23 end 24 end 25end
factorybot
1FactoryBot.define do 2 factory :class_communication do 3 text { Faker::Lorem.sentence } 4 title { Faker::Lorem.sentence } 5 class_communication_day { Faker::Date.backward } 6 association :user 7 8 after(:build) do |message| 9 message.images.attach(io: File.open('public/images/output-image1.png'), filename: 'test_image.png') 10 end 11 end 12end
views
1<%= render "shared/home_header" %> 2 3 <div class="class-communication-main"> 4 5 <div class="class-communication-left"> 6 <% if current_user.number_id == 52 %> 7 <div class="new-class-communication"> 8 <%= link_to "学級通信を書く", new_class_communication_path %> 9 </div> 10 <% end %> 11 12 <div class="left-class-communication-title"> 13 <p>これまでの学級通信</p> 14 </div> 15 16 <div class="left-class-communication-index"> 17 <% @class_communications.each do |class_communication|%> 18 <% if class_communication.user.grade_id == current_user.grade_id && class_communication.user.classroom_id == current_user.classroom_id %> 19 <div class="class-communication-index"> 20 <div class="class-communication-day"> 21 <%= link_to class_communication.class_communication_day, class_communication_path(class_communication)%> 22 </div> 23 24 <div class="class-communication-title"> 25 <%= link_to class_communication.title, class_communication_path(class_communication)%> 26 </div> 27 </div> 28 <% end %> 29 <% end %> 30 </div> 31 </div> 32 33 <div class="class-communication-right"> 34 <div class="search-index"> 35 <%= form_with(url: search_class_communications_path, local: true, method: :get, class: "search-form") do |form| %> 36 <%= form.text_field :keyword, placeholder: "学級通信を検索する", class: "search-input" %> 37 <%= form.submit "検索", class: "search-btn" %> 38 <% end %> 39 </div> 40 <% @class_communications.each do |class_communication| %> 41 <% if class_communication.user.grade_id == current_user.grade_id && class_communication.user.classroom_id == current_user.classroom_id %> 42 <%= link_to class_communication_path(class_communication) , class: "link-font" do %> 43 <div class="class-communication-detail"> 44 <div class="image-data"> 45 <div class="top-detail-class-communication"><%= class_communication.class_communication_day %></div> 46 <div class="image-count">(写真の枚数:<%= class_communication.images.count %>枚)</div> 47 </div> 48 <div class="detail-left-class-communication"> 49 <% class_communication.images.each do |image| %> 50 <div class="detail-pic"> 51 <%= image_tag image.variant(resize: '680x680') %> 52 </div> 53 54 <% end %> 55 </div> 56 57 <div class="detail-class-communication"> 58 <div class="second-detail-class-communication">タイトル:<%= class_communication.title %></div> 59 <div class="third-detail-class-communication"><%= class_communication.text %></div> 60 </div> 61 </div> 62 <% end %> 63 <% end %> 64 <% end %> 65 </div> 66</div> 67 68<%= render "shared/home_footer" %>
controller
1class ClassCommunicationsController < ApplicationController 2 before_action :authenticate_user! 3 before_action :find_class_communication_params, only: [:show, :edit, :update, :delete] 4 before_action :move_to_index, except: [:index, :show, :search] 5 6 def index 7 @class_communications = ClassCommunication.all 8 end 9 10 def new 11 @class_communication = ClassCommunication.new 12 end 13 14 def create 15 @class_communication = ClassCommunication.new(class_communication_params) 16 if @class_communication.valid? 17 @class_communication.save 18 redirect_to class_communications_path 19 else 20 render :new 21 end 22 end 23 24 def show 25 end 26 27 def search 28 @class_communications = ClassCommunication.search(params[:keyword]) 29 end 30 31 def edit 32 end 33 34 def update 35 @class_communication.update(class_communication_params) 36 if @class_communication.valid? 37 redirect_to class_communications_path 38 else 39 render :edit 40 end 41 end 42 43 def destroy 44 @class_communication.destroy 45 redirect_to class_communications_path 46 end 47 48 private 49 50 def class_communication_params 51 params.require(:class_communication).permit(:class_communication_day, :title, :text, 52 images: []).merge(user_id: current_user.id) 53 end 54 55 def find_class_communication_params 56 @class_communication = ClassCommunication.find(params[:id]) 57 end 58 59 def move_to_index 60 unless current_user.number_id == 52 61 redirect_to class_communications_path 62 end 63 end 64 65end 66
rspec
1require 'rails_helper' 2describe ClassCommunicationsController, type: :request do 3 4 before do 5 @user = FactoryBot.create(:user) 6 sign_in @user 7 @class_communication = FactoryBot.create(:class_communication) 8 end 9 10 describe 'GET #index' do 11 it 'indexアクションにリクエストすると正常にレスポンスが返ってくる' do 12 get class_communications_path 13 expect(response.status).to eq 200 14 end 15 16 it 'indexアクションにリクエストするとレスポンスに学級通信のタイトルが存在する' do 17 get class_communications_path 18 expect(response.body).to include(@class_communication.title) 19 end 20 21 22 end 23end
試したこと
binding.pryでデータを確認すると@userと@class_communicationは正常に生成されていた。veiwsに反映されていないので,@calss_communicationsとしたが、違うエラーが発生した。どなたかヒントをいただけると助かります。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/08/20 03:19
2021/08/20 03:22
2021/08/20 03:32