Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
710 views
in Technique[技术] by (71.8m points)

ruby - Testing error pages in Rails with Rspec + Capybara

In Rails 3.2.9 I have custom error pages defines like this:

# application.rb
config.exceptions_app = self.routes

# routes.rb
match '/404' => 'errors#not_found'

Which works like expected. When I set config.consider_all_requests_local = false in development.rb I get the not_found view when visiting /foo

But how do I test this with Rspec + Capybara?

I've tried this:

# /spec/features/not_found_spec.rb
require 'spec_helper'
describe 'not found page' do
  it 'should respond with 404 page' do
    visit '/foo'
    page.should have_content('not found')
  end
end

When I run this spec I get:

1) not found page should respond with 404 page
  Failure/Error: visit '/foo'
  ActionController::RoutingError:
    No route matches [GET] "/foo"

How can I test this?

Edit:

Forgot to mention: I've set config.consider_all_requests_local = false in test.rb

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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/


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...