Skip to content

Commit

Permalink
test: OmniAuth.config.mock_authの設定方法を変更 (#66)
Browse files Browse the repository at this point in the history
以前の設定方法で複数ファイルのテストを同時に実行すると、invalidのmockが後続のテストファイルに引き継がれてテストが落ちていた
  • Loading branch information
djkazunoko committed Apr 9, 2024
1 parent 893f10a commit daf4548
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
7 changes: 6 additions & 1 deletion spec/requests/user_sessions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
require 'rails_helper'

RSpec.describe 'UserSessions', type: :request do
before do
Rails.application.env_config['omniauth.auth'] = github_mock
end

describe 'POST /create' do
context 'with valid parameters' do
it 'creates a new user' do
Expand All @@ -24,7 +28,7 @@

context 'with invalid parameters' do
before do
OmniAuth.config.mock_auth[:github] = :invalid_credentials
Rails.application.env_config['omniauth.auth'] = github_invalid_mock
end

it 'does not create a new user' do
Expand All @@ -48,6 +52,7 @@
describe 'DELETE /destroy' do
it 'removes user id from session' do
get '/auth/github/callback'
expect(session[:user_id]).to be_present
delete '/logout'
expect(session[:user_id]).to be_nil
end
Expand Down
9 changes: 7 additions & 2 deletions spec/support/omniauth.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# frozen_string_literal: true

RSpec.configure do
OmniAuth.config.test_mode = true
OmniAuth.config.test_mode = true

def github_mock
OmniAuth.config.mock_auth[:github] = OmniAuth::AuthHash.new({
provider: 'github',
uid: '123545',
Expand All @@ -11,3 +12,7 @@
}
})
end

def github_invalid_mock
OmniAuth.config.mock_auth[:github] = :invalid_credentials
end
6 changes: 5 additions & 1 deletion spec/system/users_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
require 'rails_helper'

RSpec.describe 'Users', type: :system do
before do
Rails.application.env_config['omniauth.auth'] = github_mock
end

describe 'user authentication' do
context 'when authentication is successful' do
it 'allows users to login' do
Expand Down Expand Up @@ -40,7 +44,7 @@

context 'when authentication is failed' do
before do
OmniAuth.config.mock_auth[:github] = :invalid_credentials
Rails.application.env_config['omniauth.auth'] = github_invalid_mock
end

it 'redirects to root_path' do
Expand Down

0 comments on commit daf4548

Please sign in to comment.