0

I was running a test and I got the error undefined method `node_name' for nil:NilClass.

I added some javascript to one of my files so that instead of going to the new page when you want to make a new post it provides a popup with the form for creating a new post from the current page. Everything works fine however, now when I run my test with minitest capybara I get that error listed above. I'm pretty interested to know how to solve this problem. Here is a copy of my test.

require "test_helper"

feature "as a student I want a working blog so people can post" do
  scenario "User can make a post" do
    dude_sign_up
    dude_log_in
    visit posts_path
    click_on "New Post"
    create_post
    page.must_have_content "Post was successfully created"
  end
end

the methods described above from my test helper file

def dude_sign_up
  visit new_user_path
  fill_in "Name", with: "thedude"
  fill_in "Email", with: "thedude@cool.com"
  fill_in "Password", with: 'password'
  fill_in "Bio", with: "the bio"
  fill_in "Password confirmation", with: 'password'
  click_on "Submit"
end

def dude_log_in
  visit new_session_path
  fill_in "Email", with: "thedude@cool.com"
  fill_in "Password", with: 'password'
  click_on "Log In"
end

def create_post
  fill_in "Title", with: "this is a test title"
  fill_in "Content", with: "oh how this is some crazzzzy content"
  click_on "Create Post"
end

post/index.html.erb

<button class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
  New Post
</button>

<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <h4 class="modal-title" id="myModalLabel">New Post</h4>
      </div>
      <div class="modal-body">
        <%= render 'form' %>
      </div>
    </div><!-- /.modal-content -->
  </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

and also Here is my posts/_form.html.erb

<%= form_for @post, :html => {:multipart => true} do |f| %>
  <div class="field">
    <%= f.label :title %><br>
    <%= f.text_field :title %>
  </div>
  <div class="field">
    <p> upload an image if you like</p>
    <%= f.file_field :image %>
  </div>
  <div class="field">
   <%= f.label :content %><br>
   <%= f.text_area :content %>
  </div>
    <h1>Select Category</h1>
    <%= hidden_field_tag "post[category_ids][]", nil%>
    <% Category.all.each do |category| %><br>
      <%= check_box_tag "post[category_ids][]", category.id, @post.category_ids.include?(category.id), id: dom_id(category)%>
      <%= label_tag dom_id(category), category.name %>
    <% end %>
    <br>
      <div class="actions">
      <%= f.submit %>
  </div>
<% end %>

full error for the test

test_0002_User can make a post                            0:00:01.081 ERROR
        undefined method `node_name' for nil:NilClass
        Exception `NoMethodError' at:
        /Users/cheatermoves/.rvm/gems/ruby-2.0.0-p247/gems/capybara-2.1.0/lib/capybara/node/simple.rb:58:in `tag_name'
        /Users/cheatermoves/.rvm/gems/ruby-2.0.0-p247/gems/capybara-2.1.0/lib/capybara/node/simple.rb:46:in `[]'
        /Users/cheatermoves/.rvm/gems/ruby-2.0.0-p247/gems/capybara-2.1.0/lib/capybara/rack_test/node.rb:11:in `[]'
        /Users/cheatermoves/.rvm/gems/ruby-2.0.0-p247/gems/capybara-2.1.0/lib/capybara/rack_test/form.rb:81:in `method'
        /Users/cheatermoves/.rvm/gems/ruby-2.0.0-p247/gems/capybara-2.1.0/lib/capybara/rack_test/form.rb:71:in `submit'
        /Users/cheatermoves/.rvm/gems/ruby-2.0.0-p247/gems/capybara-2.1.0/lib/capybara/rack_test/node.rb:55:in `click'
        /Users/cheatermoves/.rvm/gems/ruby-2.0.0-p247/gems/capybara-2.1.0/lib/capybara/node/element.rb:118:in `block in click'
        /Users/cheatermoves/.rvm/gems/ruby-2.0.0-p247/gems/capybara-2.1.0/lib/capybara/node/base.rb:81:in `synchronize'
        /Users/cheatermoves/.rvm/gems/ruby-2.0.0-p247/gems/capybara-2.1.0/lib/capybara/node/element.rb:118:in `click'
        /Users/cheatermoves/.rvm/gems/ruby-2.0.0-p247/gems/capybara-2.1.0/lib/capybara/node/actions.rb:13:in `click_link_or_button'
        /Users/cheatermoves/.rvm/gems/ruby-2.0.0-p247/gems/capybara-2.1.0/lib/capybara/session.rb:354:in `block (2 levels) in <class:Session>'
        /Users/cheatermoves/.rvm/gems/ruby-2.0.0-p247/gems/capybara-2.1.0/lib/capybara/dsl.rb:51:in `block (2 levels) in <module:DSL>'
        test/features/blog_system_works_test.rb:15:in `block (2 levels) in <top (required)>

'

  • Can you post a complete stack trace for that error? – PinnyM Jan 10 '14 at 04:40
  • sorry my bad just posted the full error –  Jan 10 '14 at 04:51
  • The trace points to line 15 of `test/features/blog_system_works_test.rb` as the line causing this error - can you point out which line of code this is? – PinnyM Jan 10 '14 at 04:54
  • click_on "New Post". I suppose it makes sense to see the error is coming from there because that is the javascript button and I just added it and now it points to it as the error. The click_on "New Post" is what makes the popup come up with the form for creating a new post. It does work locally though. –  Jan 10 '14 at 05:02
  • Try adding a call to wait for ajax just before this line. Google is your friend here... – PinnyM Jan 10 '14 at 05:07

1 Answers1

0

Your stack trace shows that you're using the default RackTest driver for Capybara. RackTest does not support JavaScript.

To use JavaScript, you can tag your test with :js => true:

require "test_helper"

feature "as a student I want a working blog so people can post", :js => true do
  scenario "User can make a post" do
    dude_sign_up
    dude_log_in
    visit posts_path
    click_on "New Post"
    create_post
    page.must_have_content "Post was successfully created"
  end
end

If all of your tests will require JS, you can change the default driver in your spec_helper.rb by running Capybara.default_driver = :selenium

See https://github.com/jnicklas/capybara#selecting-the-driver for detailed instructions.

Tim Moore
  • 8,958
  • 2
  • 23
  • 34
  • thanks Tim problem solved. You fixed half the problem the other half was updating the gem selenium-webdriver to 2.38.0. –  Jan 11 '14 at 04:45
  • when you see the test happen it kind of feels like a fun rollercoaster –  Jan 11 '14 at 04:46