Search code examples
ruby-on-railsruby-on-rails-3.2factory-botrspec-rails

Undefined method `name/each' for nil:NilClass whilst testing index view with associations


I have the association that 'one client has many books'.

Instead of the index view of books showing client_id => 1, I have edited it to show the client's name; it works, but the test says different.

I am trying to seed the test database with an example client and book, but I cannot seem to get it working.

Can you see where I am going wrong?

I think it has something to do with 'nil:NilClass'.

spec/views/books/index.html.erb_spec.rb

require 'spec_helper'

describe "books/index" do
  before(:each) do
    assign(:clients, [
      stub_model(Client,
        :name => "Name",
        :email => "Email Address",
      )
    ])
    assign(:books, [
      stub_model(Book,
        :title => "Title",
        :client_id => 1
      )
    ])
  end

  it "renders a list of books" do
    render
    # Run the generator again with the --webrat flag if you want to use webrat matchers
    assert_select "tr>td", :text => "Title".to_s, :count => 2
    assert_select "tr>td", :text => 1.to_s, :count => 2
  end
end

app/models/book.rb

class Book < ActiveRecord::Base
  belongs_to :client

  attr_accessible :title, :client_id

  validates :title, presence: true

end

app/models/client.rb

class Client < ActiveRecord::Base
  has_many :books

  attr_accessible :name, :email

  before_save { |user| user.email = email.downcase }

  validates :name,  presence: true, length: { maximum: 50 }

  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
  validates :email, presence: true, format: { with: VALID_EMAIL_REGEX },
    uniqueness: { case_sensitive: false }

end

app/controllers/books_controller.rb (snippet)

class BooksController < ApplicationController

  # GET /books
  # GET /books.json
  def index
    @books = Book.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @books }
    end
  end

end

app/views/books/index.html.erb

<h1>Listing books</h1>

<table>
  <tr>
    <th>Title</th>
    <th>Client Name</th>
    <th></th>
    <th></th>
    <th></th>
  </tr>

<% @books.each do |book| %>
  <tr>
    <td><%= book.title %></td>
    <td><%= book.client.name %></td>
    <td><%= link_to 'Show', book %></td>
    <td><%= link_to 'Edit', edit_book_path(book) %></td>
    <td><%= link_to 'Destroy', book, method: :delete, data: { confirm: 'Are you sure?' } %></td>
  </tr>
<% end %>
</table>

<br />

<%= link_to 'New Book', new_book_path %>

Test output

  1) books/index renders a list of books
     Failure/Error: render
     ActionView::Template::Error:
       undefined method `name' for nil:NilClass
     # ./app/views/books/index.html.erb:15:in `_app_views_books_index_html_erb__##########_#####'
     # ./app/views/books/index.html.erb:12:in `_app_views_books_index_html_erb__##########_#####'
     # ./spec/views/books/index.html.erb_spec.rb:20:in `(root)'

Using Factories

If instead I use FactoryGirl, I get a similar error "undefined method 'each' for nil:NilClass".

spec/views/books/index.html.erb_spec.rb

require 'spec_helper'

describe "books/index" do
  before do
    FactoryGirl.create(:book)
  end

  it "renders a list of books" do
    render
    # Run the generator again with the --webrat flag if you want to use webrat matchers
    assert_select "tr>td", :text => "Title".to_s, :count => 2
    assert_select "tr>td", :text => 1.to_s, :count => 2
  end
end

Test output

  1) books/index renders a list of books
     Failure/Error: render
     ActionView::Template::Error:
       undefined method `each' for nil:NilClass
     # ./app/views/books/index.html.erb:12:in `_app_views_books_index_html_erb___##########_#####'
     # ./spec/views/books/index.html.erb_spec.rb:9:in `(root)'

Solution

  • First Error

    undefined method `name' for nil:NilClass
         # ./app/views/books/index.html.erb:15:in `_app_views_books_index_html_erb__##########_#####'
    

    Which refers to line 15 of app/views/books/index.html.erb, is because there isn't an entry field called 'name'. So then the interpreter looks for a method of similar name, but can't find that; hence the 'undefined method' error. The reason why there isn't an entry with that field name is because the entry failed to save at the beginning of the test - the email provided "Email Address" isn't a valid email address. You will see there is a validator at line 11 of app/models/client.rb.


    Using Factories Error

    undefined method `each' for nil:NilClass
         # ./app/views/books/index.html.erb:12:in `_app_views_books_index_html_erb___##########_#####'
    

    Which refers to line 12 of app/views/books/index.html.erb is because only one entry of book is created at line 5 of spec/views/books/index.html.erb_spec.rb. There needs to be more than one for .each to be utilised, as @chrisgeeq points out.