0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

RSpecでauthenticate_userを突破する

Last updated at Posted at 2021-08-09

##前提
・以下にアクセスできるかテストしたい
・アクセス先でcurrent_userを用いた処理をしているため、サインインした状態でいたい

hotel GET    /hotels/:id(.:format)     hotels#show

##やっていこう

#spec/rails_helper.rb
RSpec.configure do |config|
  # テスト時にdeviseのヘルパーを呼び出すことができる => sign_inが使えるになる
  config.include Devise::Test::IntegrationHelpers
  # gemのベースであるwardenのプロキシサーバーのエラーが起こるための対処法
  config.include Devise::Test::ControllerHelpers
end
#spec/controllers/hotels_controller_spec.rb
require 'rails_helper'

RSpec.describe HotelsController, type: :controller do

  describe "GET #show" do
    before do
      @user = FactoryBot.create(:user)
    end

    it "returns http success" do
      # 先ほどdeviseのヘルパーを呼び出せるようにしたため使える
      sign_in @user
      # get :show, id: @user.id だとidを渡せなかった
      get :show, params: { id: @user.id }
      expect(response).to have_http_status(:success)
    end
  end

end

参考記事

Rails RSpec コントローラーテストでログイン状態にする
【Rspec】Devise::MissingWardenエラーの解決策
Rails5の勉強: unknown keyword: idについて

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?