【问题标题】:Testing error pages in Rails with Rspec + Capybara使用 Rspec + Capybara 在 Rails 中测试错误页面
【发布时间】:2012-12-21 19:44:38
【问题描述】:

在 Rails 3.2.9 中,我有这样的自定义错误页面定义:

# application.rb
config.exceptions_app = self.routes

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

效果符合预期。当我在development.rb 中设置config.consider_all_requests_local = false 时,我在访问/foo 时得到not_found 视图

但是如何使用 Rspec + Capybara 进行测试呢?

我试过了:

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

当我运行这个规范时,我得到:

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

我该如何测试这个?

编辑:

忘了说:我在test.rb中设置了config.consider_all_requests_local = false

【问题讨论】:

    标签: ruby-on-rails ruby rspec capybara


    【解决方案1】:

    test.rb 中有问题的设置不仅仅是

    consider_all_requests_local = false
    

    还有

    config.action_dispatch.show_exceptions = true
    

    如果你设置了这个,你应该能够测试错误。 我无法在环绕过滤器中使用它。

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

    【讨论】:

    • 谢谢,这正是我所缺少的。我认为问题比这更深,所以我开始在水豚周围挖掘......
    • 控制器规格怎么样?通过这些更改,我的功能规范工作正常,但我仍然在控制器规范中得到失败的测试(带有 ActiveRecord::RecordNotFound 错误)。我需要为此做些不同的事情吗?
    • 链接已损坏,现在位于agileleague.com/blog/…
    • 如果你不想在 test.rb 文件中设置这个,你可以查看这个解决方案stackoverflow.com/a/9941128/2989852
    【解决方案2】:

    config.consider_all_requests_local = false 设置需要在 config/environments/test.rb 中设置,就像您为开发设置所做的那样。

    如果您不想对所有测试都执行此操作,那么 rspec around filter 可能有助于在测试前设置状态并在测试后恢复,如下所示:

    # /spec/features/not_found_spec.rb
    require 'spec_helper'
    describe 'not found page' do
      around :each do |example|
         Rails.application.config.consider_all_requests_local = false
         example.run
         Rails.application.config.consider_all_requests_local = true
      end
    
      it 'should respond with 404 page' do
        visit '/foo'
        page.should have_content('not found')
      end
    end
    

    【讨论】:

    • 我忘了说,我已经尝试在test.rb 中设置consider_all_requests_local = false
    • 对我来说,我也有 ActionController::RoutingError:
    【解决方案3】:

    如果您想这样做并且不想更改config/environments/test.rb,您可以按照此post 中的解决方案进行操作。

    【讨论】:

      【解决方案4】:

      可以直接访问404错误页面:

      访问/404 而不是/foo

      【讨论】:

        【解决方案5】:

        使用 Rails 5.2,Capybara 3 我能够使用以下方法模拟页面错误

        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
        

        更新

        在运行完整的测试套件时,这似乎并不总是有效。所以我不得不在config/environments/test.rb 中设置config.action_dispatch.show_exceptions = true 并删除周围的块。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-03-13
          相关资源
          最近更新 更多