我可以使用capybara进行一个非常简单的集成测试,以测试页面是否正确呈现。不幸的是,如果测试恰好在错误页面中找到所需内容,即使出现错误,测试也可以通过。
示例测试:
scenario 'goes to "new booking" page' do
visit '/bookings/new'
expect(page).to have_content('Driver')
end
这是一个失败测试输出的示例,只是因为测试的字符串不在错误页面中而失败:
Failure/Error: expect(page).to have_content('Invited')
expected to find text "Invited" in "TypeError at /bookings/86703360-4d92-4b79-bdfd-bbfbf843143e =========================================================== > no implicit conversion of nil into String app/models/vehicle.rb, line 11 ------------------------------ ``` ruby 6 has_many :drivers, through: :workables 7 8 has_and_belongs_to_many :job_types 9 10 def name_with_user > 11 name + \" \" + name_of_user 12 end 13 14 def name_with_user_and_jobs 15 name + \" \" + name_of_user + \" jobs\" 16 end ``` App backtrace ------------- - app/models/vehicle.rb:11:in `name_with_user' - app/views/bookings/show.html.erb:17:in `_app_views_bookings_show_html_erb__3294924100850065400_70296497966640' - app/controllers/bookings_controller.rb:115:in `show' - spec/features/booking_spec.rb:18:in `block (2 levels) in ' Full backtrace -------------- - app/models/vehicle.rb:11:in `name_with_user' - app/views/bookings/show.html.erb:17:in `_app_views_bookings_show_html_erb__3294924100850065400_70296497966640' - actionpack (4.0.8) lib/action_view/template.rb:143:in `block in render' - activesupport (4.0.8) lib/active_support/notifications.rb:161:in `instrument' - actionpack (4.0.8) lib/action_view/template.rb:141:in `render' - actionpack (4.0.8) lib/action_view/renderer/template_renderer.rb:49:in `block (2 levels) in render_template' - actionpack (4.0.8) lib/action_view/renderer/abstract_renderer.rb:38:in `block in instrument' - activesupport (4.0.8) lib/active_support/notifications.rb:159:in `block in instrument'
答案 0 :(得分:2)
您可以将文本换行到span / div并测试选择器中的文本,例如:
have_css("#status", text: "Invited") #not tested
http://www.rubydoc.info/gems/capybara/0.4.0/Capybara/Node/Matchers
答案 1 :(得分:0)
我想您可能想要使用within
。例如:
within('.some-css-class') { expect(page).to have_content 'some content' }
通过这种方式,您可以使用css选择器将搜索范围缩小到特定div或其他任何内容。