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 yourspec_helper.rb
orrails_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
How to Find the Namespace/Module Name Programmatically in Ruby on Rails
How to Fix Rubygems Recent Deprecation Warning
Project Euler 1:Find the Sum of All the Multiples of 3 or 5 Below 1000
Get Index of String Scan Results in Ruby
Missing Symbol When Installing Ruby-2.3.0 on Os X 10.11.6 by Rvm
Understanding the Gemfile.Lock File
How to Find Out Which Gem Has a Specific Dependency
Bundle Failing - Can't Find the Postgresql Client Library (Libpq)
Rails Resque Workers Fail with Pgerror: Server Closed the Connection Unexpectedly
Simple Encryption in Ruby Without External Gems
Guard with Rspec on Rails 4 Giving a Lot of Warnings
How to Remove Permission Denied @ Rb_Sysopen - Gem Install Error
How to Say Something Happened "X Minutes Ago" or "X Hours Ago" or "X Days Ago" in Ruby
Problems Installing Ruby on Mountain Lion - Ruby 1.9.3 Wont' Compile
Why Isn't 'Method=' Treated the Same as Any Other Method
How to Get Haml to Work with Rails
Convert a .Doc or .Pdf to an Image and Display a Thumbnail in Ruby