Testing Error Pages in Rails with Rspec + Capybara

Testing error pages in Rails with Rspec + Capybara

The problematic setting in the test.rb is not only the

consider_all_requests_local = false

but also

config.action_dispatch.show_exceptions = true

If you set this you should be able to test the error.
I was not able to use it in an around filter.

Have a look for http://agileleague.com/blog/rails-3-2-custom-error-pages-the-exceptions_app-and-testing-with-capybara/

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

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.

Rails app testing with RSpec and Capybara. I get Error Capybara::ElementNotFound

You have a number of issues here. Firstly you're going to the page before the customer instance has been created, which means it won't be shown on the page. This is because let lazily evaluates - see https://relishapp.com/rspec/rspec-core/v/3-8/docs/helper-methods/let-and-let - and will only create the customer the first time customer is called in the test.

visit '/' # <= no customer instance exists here
click_on customer.status.humanize # <= customer instance actually created here

You can fix that by using the non lazily evaluated version let! which will always create a customer instance, or by calling customer before calling visit

Secondly, your test is modifying the customer instance in the DB, but never reloading the instance you have in memory (so the new status would never be seen)

expect(customer.reload).to have_status "move_to_white_list"

Thirdly, If you're using a driver that supports asynchronous behavior (any driver other than rack-test) there is no guarantee any side effects caused by the action methods (click_on, etc) will have completed before the method call returns. That means your expectation will most likely get called before the customer instances status has changed. To fix that the have_status matcher would need to be written to have retrying behavior (like all the Capybara provided matchers have) and to have the reloading behavior in the matcher too. This is a major reason why it's generally not a good idea to be verifying DB changes in system/feature tests, and instead stick to verifying changes in the UI.

Capybara and Rspec. Capybara not Rendering page in Test, with visit or get method in rails

From the documentation it seems that you should tag your specs as feature or put them into the spec/features folder.

For me it seems like you have the default behavior for controller test, which is to not render anything. You could override this, but I think it'll be better to have your integration tests separate from the controller tests.

Rspec/Capybara test brings sass error (with no error on page)

It was a mistake in the bootstrap.scss file (I'm running an old version so It may be fixed in a newer one). The error was:

Color: #ff;f
Outline: none;

I fixed it to:

Color: #fff;
Outline: none;

After that the test passed without fail



Related Topics



Leave a reply



Submit