Rails test results in undefined method `id' for nil: NilClass error and cannot be resolved

Asked 2 years ago, Updated 2 years ago, 74 views

symptoms

The rails test results in an undefined method `id' for nil: NilClass error. I couldn't solve it with the name below, so please let me know.

Error: UsersProfileTest#test_profile_display:ActionView::Template::Error: undefined method id'for nil:NilClass  
app/views/likes/_like.html.erb:1:in_app_views_likeslike_html_erb_114985059599241592_62177660'app/views/microposts/_micropost.html.erb:16:in_app_views_microposts_micropost_html_624204_629402_629402_6974  
app/views/users/show.html.erb:20:in_app_views_users_show_html_erb_489431433523421794_53681160'test/integration/users_profile_test.rb:11:in`block in<class:UsersProfileTest>'

bin/rails test test/integration/users_profile_test.rb: 10

environment

cloud9,Rails 5.1.4

Source Code

---_like.html.erb---
<%if micropost.like_user(current_user.id)%>
  <%=Button_to micropost_like_path(like, micropost_id: 
micropost.id), method::delete, id: "like-button", remote:true do%> 
    <%=image_tag("icon_red_hard.png")%>
    <span>
      <%=micropost.likes_count%>
    </span>
  <%end%>
<%else%>
  <%=button_to micropost_like_path(micropost), id: "like-button", 
remote —true do%>  
    <%=image_tag("icon_red_heart.png")%>
    <span>
      <%=micropost.likes_count%>
    </span>
  <%end%>
<%end%>
---_micropost.html.erb---
<liid="micropost-<%=micropost.id%>">
  <%=link_to gravatar_for (micropost.user, size: 50), micropost.user%>
  <span class="user">%=link_to micropost.user.name, micropost.user%>/span>
  <span class="content">
    <%=micropost.content%>
    <%=image_tag micropost.picture.url if micropost.picture?%>    
  </span>
  <span class="timestamp">
    Posted<%=time_ago_in_words(micropost.created_at)%>ago.
    <%if current_user?(micropost.user)%>
      <%=link_to "delete", micropost, method::delete,
                                   data: {confirm: "You sure?"}%>
    <%end%>
  </span>
  <!--like extensions-->
  <%=render partial: 'like/like', locals: {micropost:micropost, 
likes:@likes}%>
</li>
---show.html.erb---
<%provide(:title,@uu-ser.name)%>
<div class="row">
  <aside class="col-md-4">
    <section class="user_info">
      <h1>
        <%=gravatar_for@user%>
        <%=@u ser.name%>
      </h1>
    </section>
    <section class="stats">
        <%=render'shared/stats'%>
      </section>
   </aside>

  <div class="col-md-8">
    <%=render'follow_form' if logged_in?%>
    <%[email protected] y?%>
      <h3>Microposts(<%[email protected]%>)</h3>
      <ol class="microposts">
        <%=render@microposts%>
      </ol>
      <%=will_paginate@microposts%>
    <%end%>
  </div>
</div>
---users_profile_test.rb---
require'test_helper'

class UsersProfileTest <ActionDispatch::IntegrationTest
  include ApplicationHelper

  def setup
    @user=users(:michael)
  end

  test "profile display" do
    get user_path(@user)
    assert_template 'users/show'
    assert_select'title', full_title(@u ser.name)
    assert_select 'h1', text: @u ser.name
    assert_select'h1>img.gravatar'
    [email protected]_s,response.body
    assert_select 'div.pagination'
    @ user.microposts.paginate(page:1).each do | micropost |
      assert_match micropost.content, response.body
    end
  end
end

Tried

  • I looked it up online and saw Q&A that the undefined method id'ornil:NilClass' error was displayed because the user was not registered, so I signed up for the user, but I couldn't troubleshoot it.
  • Match a sample code for the source code that appears to be getting an error.

ruby-on-rails ruby

2022-09-30 21:31

1 Answers

current_user is nil, so current_user.id is probably an error.

If you are using Device, you can use a helper to reproduce your login status.

class UsersProfileTest<ActionDispatch::IntegrationTest
  include ApplicationHelper
  includeDevice::Test::IntegrationHelpers

  test "profile display" do
    sign_in@user
    get user_path(@user)


2022-09-30 21:31

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.