LoginSignup
3
0

More than 3 years have passed since last update.

【FeatureSpec】404 / 500ページを表示させる

Last updated at Posted at 2019-07-04

まずconfig/environments/test.rb内の下記2つの設定を変更してください。
初期状態ではtrueとfalseが反対になっていると思われます。
現時点では、エラーが起きた時、開発用のエラー画面になりますので、テスト時に本番と同じエラー画面を表示するための設定です。

config/environments/test.rb
config.consider_all_requests_local = false
config.action_dispatch.show_exceptions = true

次にFeatureSpecのテスト項目です。

# 省略

  scenario '404エラーページに遷移し、ステータスコードが404であること' do
    # 存在しないパスにアクセスする
    visit '/tasks/404test'

    expect(page).to have_content '404 not found'
    expect(page.status_code).to eq 404
  end

  scenario '500エラーページに遷移し、ステータスコードが500であること' do
    # 一覧画面に遷移したら例外を発生させる
    allow_any_instance_of(TasksController).to receive(:index).and_throw(Exception)
    visit tasks_path

    expect(page).to have_content '500 Internal Server Error'
    expect(page.status_code).to eq 500
  end

# 省略

参考リンク

使えるRSpec入門・その3「ゼロからわかるモック(mock)を使ったテストの書き方」
https://qiita.com/jnchito/items/640f17e124ab263a54dd
 ※例外発生方法を参考にしました(allow_any_instance_of)

3
0
0

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
3
0