Sean Magyar
Sean Magyar

Reputation: 2380

rspec nil class error in controller spec

I have a profile model with avatar attr and full_name method that uses first_name and last_name attrs. All of these are delegated in user model. I don't really understand how I can get the nil class error. If I delete the image tag line the tests pass, so user can't be nil since I also call user.full_name. I also have other models where I use the avatar and full_name and the specs are working fine there. It works in dev env too.

What did I miss here?

error for all the 3 it blocks:

Failure/Error: <%= image_tag user.avatar.url(:small_thumb), class: "profile-index-avatar" %>

ActionView::Template::Error:
  undefined method `url' for nil:NilClass
  Did you mean?  URI

show.html.erb

<% @product.users.each_slice(2) do |user_row| %>
  <div class="row" style="padding-top:20px;">
    <% user_row.each do |user| %>
      <%= link_to user_profile_path(user) do %>
        <div class="col-md-6 product-user-column">
          <%= image_tag user.avatar.url(:small_thumb), class: "profile-index-avatar" %>
          <%= user.full_name %>
        </div>
      <% end %>
    <% end %>
  </div>
<% end %>

controller spec

describe "GET show" do
  let!(:profile) { create(:profile, user: @user) }
  let!(:product) { create(:product, :product_with_nested_attrs) }
  let!(:product_user) { create(:product_user, user: @user, product: product, role: "owner") }
  before(:each) do
    get :show, id: product
  end

  it "assigns products" do
    expect(assigns(:product)).to eq(product)
    expect(assigns(:product).industry_products.size).to eq(1)
  end

  it { is_expected.to respond_with 200 }
  it { is_expected.to render_template :show }
end

UPDATE

Based on zetetic's suggestions I checked if avatar is nil. The user avatar is not nil for the index action neither the avatar for the show action.

If put the "avatar not nil for show" method in while show page contains image tag then it raises the same error just like for the rest. If I delete the image tag line then it says it's not nil.

describe "GET index" do
  let!(:profile) { create(:profile, user: @user) }
  let!(:product) { create(:product, :product_with_nested_attrs) }
  before(:each) do
    get :index
  end

  it "avatar not nil for show" do
    expect(@user.avatar).to_not be_nil
  end

  it "assigns products" do
    expect(assigns(:products)).to eq([product])
  end

  it { is_expected.to respond_with 200 }
  it { is_expected.to render_template :index }
end

describe "GET show" do
  let!(:profile) { create(:profile, user: @user) }
  let!(:product_user) { create(:product_user, user: @user, product: product, role: "owner") }
  let!(:product) { create(:product, :product_with_nested_attrs) }
  before(:each) do
    get :show, id: product
  end

  it "avatar not nil for show" do
    expect(@user.avatar).to_not be_nil
  end

  it "assigns products" do
    expect(assigns(:product)).to eq(product)
    expect(assigns(:product).industry_products.size).to eq(1)
  end

  it { is_expected.to respond_with 200 }
  it { is_expected.to render_template :show }
end

Upvotes: 1

Views: 445

Answers (1)

JaTo
JaTo

Reputation: 2832

avatar is nil for one of the users

Upvotes: 1

Related Questions