الاختبار باستخدام قضبان minitest وcapybara بطريقة غير محددة `node_name' لـ nil:NilClass

StackOverflow https://stackoverflow.com//questions/21036313

سؤال

كنت أجري اختبارًا وحصلت على خطأ طريقة غير محددة `node_name' لـ nil:NilClass.

لقد أضفت بعض جافا سكريبت إلى أحد ملفاتي بحيث بدلاً من الانتقال إلى الصفحة الجديدة عندما تريد إنشاء منشور جديد، فإنه يوفر نافذة منبثقة تحتوي على نموذج لإنشاء منشور جديد من الصفحة الحالية.كل شيء يعمل بشكل جيد، ولكن الآن عندما أقوم بإجراء الاختبار باستخدام minitest capybara، يظهر لي هذا الخطأ المذكور أعلاه.أنا مهتم جدًا بمعرفة كيفية حل هذه المشكلة.هنا نسخة من اختباري.

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

الطرق الموضحة أعلاه من ملف مساعد الاختبار الخاص بي

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

وأيضا هنا منشوراتي/_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 %>

الخطأ الكامل للاختبار

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

'

هل كانت مفيدة؟

المحلول

يوضح تتبع المكدس أنك تستخدم برنامج التشغيل RackTest الافتراضي لـ Capybara.لا يدعم RackTest JavaScript.

لاستخدام JavaScript، يمكنك وضع علامة على الاختبار الخاص بك باستخدام :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

إذا كانت جميع اختباراتك تتطلب استخدام JS، فيمكنك تغيير برنامج التشغيل الافتراضي في ملف spec_helper.rb عن طريق الركض Capybara.default_driver = :selenium

يرى https://github.com/jnicklas/capybara#selecting-the-driver للحصول على تعليمات مفصلة.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top