前提
Ruby on Railsで映画のレビューサイトを作っています。
投稿したコメントに映画のタイトルを表示する機能を追加したところ下記のエラーが出ました。
機能自体はうまく動作しているのでテストの書き方に問題があると考えています。
テストの元になっているのはRailsチュートリアルです。
undefined なので定義されていないことが原因だと思いますが、どこに、どのように記述すればよいでしょうか?
ご教授いただけると嬉しいです。
実現したいこと
- 投稿したコメントに映画のタイトルを表示させる機能をテストでパスさせたい
エラーメッセージ
ERROR["test_profile_display", UsersProfileTest, 5.1500919768586755] test_profile_display#UsersProfileTest (5.15s) ActionView::Template::Error: ActionView::Template::Error: undefined method `title' for nil:NilClass app/views/comments/_comment.html.erb:5:in `_app_views_comments__comment_html_erb__857699480521752031_47037482885220' app/views/users/show.html.erb:19:in `_app_views_users_show_html_erb___3985789656075004903_47037471840320' test/integration/users_profile_test.rb:14:in `block in <class:UsersProfileTest>'
該当のソースコード
1. パーシャル _comment.html.erb
2. show.html.erb
3. users_profile_test.rb
1. パーシャル _comment.html.erb
<li id="comment-<%= comment.id %>"> <%= link_to gravatar_for(comment.user, size: 50), comment.user %> <span class="user"><%= link_to comment.user.name, comment.user %></span> <span class="content"><%= comment.content %></span> <span class="title"><%= link_to comment.movie.title, "/movies/#{comment.movie_id}" %></span> #該当箇所 <span class="timestamp"> Posted <%= time_ago_in_words(comment.updated_at) %> ago. <% if current_user?(comment.user) %> <%= link_to "edit", "/comments/#{comment.id}/edit?movie_id=#{comment.movie_id}" %> <%= link_to "delete", comment, method: :delete, data: { confirm: "You sure?" } %> <% end %> </span> </li>
2. show.html.erb
<% provide(:title, @user.name) %> <div class="row"> <aside class="col-md-4"> <section class="user_info"> <h1> <%= gravatar_for @user %> <%= @user.name %> </h1> </section> <section class="stats"> <%= render 'shared/stats' %> </section> </aside> <div class="col-md-8"> <%= render 'follow_form' if logged_in? %> <% if @user.comments.any? %> <h3>Comments (<%= @user.comments.count %>)</h3> <ol class="comments"> <%= render @comments %> #該当箇所 </ol> <%= will_paginate @comments %> <% end %> </div> </div>
3. users_profile_test.rb
equire 'test_helper' class UsersProfileTest < ActionDispatch::IntegrationTest include ApplicationHelper def setup @user = users(:michael) end test "profile display" do get login_path post login_path, params: { session: { email: @user.email, password: 'password' } } get user_path(@user) #該当箇所 assert_template 'users/show' assert_select 'title', full_title(@user.name) assert_select 'h1', text: @user.name assert_select 'h1>img.gravatar' assert_match @user.comments.count.to_s, response.body assert_select 'div.pagination' @user.comments.paginate(page: 1).each do |comment| assert_match comment.content, response.body end end end
コメント
初学者のため、至らない点がございましたら申し訳ありません。
他に必要なソースコードがありましたら、随時追加いたしますので、教えていただければと思います。
何卒よろしくお願いいたします。
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。