2
2

More than 1 year has passed since last update.

【Rspecエラー】HTTP Basic: Access denied

Posted at

環境

Ruby 3.0.2
Rails 6.1.4.1

状況

Rspec実行時、401エラーが出た

Failure/Error: expect(response).to have_http_status(200)
       expected the response to have status code 200 but it was 401

解決法

responseの中身を確認

> response.body
=> "HTTP Basic: Access denied.\n"

Basic認証を採用していたことを思い出したので、テスト環境ではBasic認証をしないように指定

app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
   before_action :basic_auth unless Rails.env.test?

   def basic_auth
    ...
   end
end

参考

2
2
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
2
2