LoginSignup
1
0

More than 1 year has passed since last update.

【omniauth-google-oauth2+devise】Rspecのリクエストテスト

Last updated at Posted at 2021-10-08

deviseのみ使用時のリクエストテストではsign_in userでログインできていたが、
omniauth-google-oauth2+devise使用時のテストの書き方で少し苦戦したので書き残す。

環境

Ruby 3.0.2
Rails 6.1.4.1

やり方

  • sign_inが使えるようにdeviseでのテストの設定をする
  • omniauth-google-oauth2が使用できるように設定
spec/rails_helper.rb
RSpec.configure do |config|
  config.include Devise::Test::IntegrationHelpers, type: :request
end

OmniAuth.configure do |c|
  c.test_mode = true
  c.mock_auth[:google_oauth2] = OmniAuth::AuthHash.new({
    "provider" => "google_oauth2",
    "uid" => "100000000000000000000",
    "info" => {
      "name" => "test employee",
      "email" => "tester1@example.com",
      "first_name" => "test",
      "last_name" => "employee"
    }
  })
end
spec/requests/XXXXX_spec.rb
RSpec.describe "/XXXXX", type: :request do
  describe 'GET /XXXXX' do
  subject { get '/XXXXX', params: { provider: "google_oauth2" } }

  before do
    Rails.application.env_config["devise.mapping"] = Devise.mappings[:user]
    Rails.application.env_config["omniauth.auth"] = OmniAuth.config.mock_auth[:google_oauth2]
    sign_in user
    subject
  end
  it 'レスポンスが200を返すこと' do
    expect(response).to have_http_status(200)
  end
end

参考

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