LoginSignup
7

More than 5 years have passed since last update.

特定のテストでのみRambulanceを有効にしてエラーページのテストをするやり方

Posted at

Rambulance でエラーページの出し分けを行っているので、エラーページのテストを書きたかった。
Rambulance をテスト環境でも動かすには、 config/environments/test.rb

config.consider_all_requests_local = false
config.action_dispatch.show_exceptions = true

を設定すれば良いのだが、これだと例外のテストが書きにくくなりそうなので、
指定した場合のみ Rambulance が有効になるようにしたかった。

試行錯誤のすえ、RSpec の場合は次のような shared_context を用意すればいけた。

RSpec.shared_context 'enable rambulance', rambulance: true do
  before do
    Rails.application.env_config['action_dispatch.show_detailed_exceptions'] = false
    Rails.application.env_config['action_dispatch.show_exceptions'] = true
  end

  after do
    Rails.application.env_config['action_dispatch.show_detailed_exceptions'] = true
    Rails.application.env_config['action_dispatch.show_exceptions'] = false
  end
end

使い方

specify 'エラーページを返すこと', rambulance: true do
  # ...
end

注意点として、 Rails.application.config の値を直接変えてもダメだったので、Rails.application.env_config を変える必要がある。

参考: http://makandracards.com/makandra/12807-custom-error-pages-in-rails-3-2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7