croceldon
croceldon

Reputation: 4595

How do I make rails errors in Capybara show up in rspec features?

I'm using capybara to run my rspec features. The issue is that when an error occurs in the rails stack while running the spec, the error doesn't "pass through" to rspec and cause the test to fail. The error page is generated and checked by rspec.

So, I don't get an immediate failure with an error on my specs. I only get an error when I look for content, or try to fill in a field, that doesn't appear on the error page.

This is not what I want. I want errors encountered in the rails stack (controller, views, etc.) to cause rspec to fail immediately.

How can I resolve this?

Upvotes: 3

Views: 1687

Answers (3)

Robert Nubel
Robert Nubel

Reputation: 7522

Capybara is capable of raising server errors as failures on your tests, provided it's hooking into Rails correctly (see: the last bullet point on the "Gotchas" section of the README).

Make sure that you:

  • Are running your Capybara tests in your Rails environment
  • Have require 'capybara/rails' in your spec_helper.rb or rails_helper.rb file
  • Do not need your actual server running in order to run the tests (if you do, this is a symptom that your setup is incorrect).
  • Are visiting relative, not absolute, URLs in your tests (e.g. visit 'http://localhost:3000/mypage is wrong, visit '/mypage' is right).

If the above are all true, and you're still seeing the behavior, something else is likely wrong with your setup. Try changing your driver back to the default (Rack::Test) and see if the issue persists.

Edit: as explained in the comments below, the issue here was that the better_errors gem was loading in the test environment, which rescued the error and displayed the pretty error page before Capybara and Rspec got a chance to see the error. Moving it to the development-only group in the Gemfile fixed the issue. This thread held the answer.

Upvotes: 3

Aravin
Aravin

Reputation: 7067

You can use following capybara method to force the scenario to fail.

fail(msg = "Error. Check log for details.")

Upvotes: 0

Jaffa
Jaffa

Reputation: 12700

Create a test case for your controller / view and test it before doing any integration tests.

That's the right way to do it :

  • unit testing for methods
  • functional testing for controllers / views
  • integration tests then

Upvotes: 1

Related Questions