我正在运行测试,我得到错误的未定义方法`node_name'为nil:NilClass。
我在我的一个文件中添加了一些javascript,这样当你想要发布一个新帖子时它不会转到新页面,而是提供一个弹出窗口,其中包含从当前页面创建新帖子的表单。一切正常但是,现在当我用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
交/ 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">×</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)>
答案 0 :(得分:0)
您的堆栈跟踪显示您正在使用Capybara的默认RackTest驱动程序。 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。