Daniel Friis
Daniel Friis

Reputation: 484

undefined method `node_name' for nil:NilClass

I'm having some problems with rspec, which returns an error, even though everything works on localhost.

  1) Item pages item creation with invalid information should not create an item
     Failure/Error: expect { click_button "Add" }.not_to change(Item, :count)
     NoMethodError:
       undefined method `node_name' for nil:NilClass
     # (eval):2:in `click_button'
     # ./spec/requests/item_pages_spec.rb:20:in `block (5 levels) in <top (required)>'
     # ./spec/requests/item_pages_spec.rb:20:in `block (4 levels) in <top (required)>'

item_pages_spec.rb

require 'spec_helper'

describe "Item pages" do

  subject { page }

  let(:user) { FactoryGirl.create(:user) }
  let(:list) { FactoryGirl.create(:list, user: user)}
  before { sign_in user }

  describe "item creation" do
    before do 
      visit user_list_path(user, list)
      click_link "Add wish"
    end

    describe "with invalid information" do

      it "should not create an item" do
        expect { click_button "Add" }.not_to change(Item, :count)
      end

      # describe "error messages" do
      #   before { click_button "Add" }
      #   it { should have_content('error') } 
      # end
    end

    describe "with valid information" do

      before { fill_in 'item_title', with: "Lorem ipsum" }
      it "should create a item" do
        expect { click_button "Add" }.to change(Item, :count).by(1)
      end
    end
  end
end

the form for creating an item

<% if signed_in? %>

  <div id="addWish" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="addWishLabel" aria-hidden="true">
    <div class="modal-header">
      <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
      <h3 id="addWishLabel">Add wish</h3>
    </div>
    <div class="modal-body">
      <%= form_for(@item) do |f| %>
      <%= render 'shared/error_messages', object: f.object %>
      <div class="field">
        <%= f.hidden_field :list_id, value: @list.id %>
        <%= f.text_field :title, placeholder: "Title..." %>
        <%= f.text_field :link, placeholder: "Link..." %>
      </div>
    </div>
    <div class="modal-footer">
      <button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
      <%= f.submit "Add", class: "btn btn-primary" %>
      <% end %>
    </div>
  </div>
<% end %>

I'm very new to rails and any help is thus much appreciated!

Upvotes: 1

Views: 1959

Answers (3)

Rennan Oliveira
Rennan Oliveira

Reputation: 96

If anyone else falls here, check your html tags as @lucas mentioned. It might be your case. I found this here. Capybara won't find your button correctly if your tags are badly closed. In my case, I had this:

    <div>
    <%= form_tag do %>
      <%= submit_tag %>
    </div>
    <% end %>

Closing the div outside of the form_tag did it for me.

Upvotes: 0

Daniel Friis
Daniel Friis

Reputation: 484

Had to change click_button "Add" to click_link "Add".

Upvotes: 2

Phobos98
Phobos98

Reputation: 456

expect { click_button "Add" }.to_not change(Item.count)

expect { click_button "Add" }.to_not change(@items, :count)

Upvotes: 0

Related Questions