Will Taylor
Will Taylor

Reputation: 2304

Capybara passes tests with errors on page

I can do a very simple integration test using capybara to test that a page renders correctly. Unfortunately, the tests can pass even if there is an error, if the test happens to find the required content in the error page.

Example test:

scenario 'goes to "new booking" page' do

  visit '/bookings/new'

  expect(page).to have_content('Driver')

end

Here's an example of the output for a failed test, that only happened to fail because the tested string was not in the error page:

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' 

Upvotes: 1

Views: 219

Answers (2)

pmichna
pmichna

Reputation: 4888

I think you may want to use within. Example:

within('.some-css-class') { expect(page).to have_content 'some content' }

This way you can narrow your search to a particular div or anything else using css selectors.

Upvotes: 0

Michal
Michal

Reputation: 753

You can wrap the text in span/div and test for the text in selector, for eg:

have_css("#status", text: "Invited") #not tested

http://www.rubydoc.info/gems/capybara/0.4.0/Capybara/Node/Matchers

Upvotes: 2

Related Questions