Pregunta

Estaba ejecutando una prueba y recibí el error método indefinido `node_name' para nil:NilClass.

Agregué algo de javascript a uno de mis archivos para que, en lugar de ir a la nueva página cuando quieras hacer una nueva publicación, proporcione una ventana emergente con el formulario para crear una nueva publicación desde la página actual.Todo funciona bien, sin embargo, ahora, cuando ejecuto mi prueba con minitest capibara, aparece el error mencionado anteriormente.Estoy bastante interesado en saber cómo resolver este problema.Aquí hay una copia de mi prueba.

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

los métodos descritos anteriormente desde mi archivo auxiliar de prueba

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

publicación/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 -->

y también Aquí están mis publicaciones/_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 %>

error completo para la prueba

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)>

'

¿Fue útil?

Solución

El seguimiento de su pila muestra que está utilizando el controlador RackTest predeterminado para Capybara.RackTest no es compatible con JavaScript.

Para usar JavaScript, puede etiquetar su prueba con :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

Si todas sus pruebas requerirán JS, puede cambiar el controlador predeterminado en su spec_helper.rb mediante la ejecución Capybara.default_driver = :selenium

Ver https://github.com/jnicklas/capybara#selecting-the-driver para obtener instrucciones detalladas.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top