Testing error pages in Rails with Rspec + Capybara

后端 未结 5 2140
Happy的楠姐
Happy的楠姐 2020-12-05 10:36

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#n         


        
5条回答
  •  感情败类
    2020-12-05 11:08

    With Rails 5.2, Capybara 3 I was able to simulate page errors with the following

    around do |example|
      Rails.application.config.action_dispatch.show_exceptions = true
      example.run
      Rails.application.config.action_dispatch.show_exceptions = false
    end
    
    before do
      allow(Person).to receive(:search).and_raise 'App error simulation!'
    end
    
    it 'displays an error message' do
      visit root_path
      fill_in 'q', with: 'anything'
      click_on 'Search'
      expect(page).to have_content 'We are sorry, but the application encountered a problem.'
    end
    

    Update

    This seems to not always work when running the full test suite. So I had to set config.action_dispatch.show_exceptions = true in config/environments/test.rb and remove the around block.

提交回复
热议问题