現象

rails testを実行するとundefined method `id' for nil:NilClassエラーが発生する。 下名では解決できませんでしたので、ご教授願います。

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_erb__3344069742097572585_62280280'  
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

環境

cloud9, Rails 5.1.4

ソースコード

---_like.html.erb---
<% if micropost.like_user(current_user.id) %>
  <%= button_to micropost_like_path(likes, micropost_id: 
micropost.id), method: :delete, id: "like-button", remote: true do %> 
    <%= image_tag("icon_red_heard.png") %>
    <span>
      <%= micropost.likes_count %>
    </span>
  <% end %>
<% else %>
  <%= button_to micropost_likes_path(micropost),id: "like-button", 
remote: true do %>  
    <%= image_tag("icon_red_heart.png") %>
    <span>
      <%= micropost.likes_count %>
    </span>
  <% end %>
<% end %>
---_micropost.html.erb---
<li id="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拡張機能-->
  <%= render partial: 'likes/like', locals: { micropost: micropost, 
likes: @likes } %>
</li>
---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.microposts.any? %>
      <h3>Microposts (<%= @user.microposts.count %>)</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(@user.name)
    assert_select 'h1', text: @user.name
    assert_select 'h1>img.gravatar'
    assert_match @user.microposts.count.to_s, response.body
    assert_select 'div.pagination'
    @user.microposts.paginate(page: 1).each do |micropost|
      assert_match micropost.content, response.body
    end
  end
end

試したこと

  • ネットで調べて、undefined methodid'or nil:NilClass`エラーはユーザー登録がされていないため表示されているというQ&Aを見たので、ユーザーのsignupを行なったがトラブルシュートできず。
  • エラーが出ていると思われるソースコードのサンプルコードと照合。