Skip to content

Commit

Permalink
test: User.find_or_create_from_auth_hash!メソッドのテストを作成 (#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
djkazunoko committed Apr 6, 2024
1 parent 19905bd commit 3378e5d
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,53 @@
user2 = FactoryBot.build(:user, name: 'tester2', image_url: 'example.com/2', provider: 'twitter')
expect(user2).to be_valid
end

describe '.find_or_create_from_auth_hash!' do
let(:auth_hash) do
{
provider: 'github',
uid: '0001',
info: {
nickname: 'testuser',
image: 'https://example.com/testuser.png'
}
}
end

context 'when user does not exist' do
it 'creates a new user' do
expect do
User.find_or_create_from_auth_hash!(auth_hash)
end.to change(User, :count).by(1)
end

it 'returns the created user' do
user = User.find_or_create_from_auth_hash!(auth_hash)
expect(user).to be_a(User)
expect(user.provider).to eq('github')
expect(user.uid).to eq('0001')
expect(user.name).to eq('testuser')
expect(user.image_url).to eq('https://example.com/testuser.png')
end
end

context 'when user already exists' do
let!(:existing_user) { FactoryBot.create(:user) }

it 'does not create a new user' do
expect do
User.find_or_create_from_auth_hash!(auth_hash)
end.not_to change(User, :count)
end

it 'returns the existing user' do
user = User.find_or_create_from_auth_hash!(auth_hash)
expect(user).to be_a(User)
expect(user.provider).to eq('github')
expect(user.uid).to eq('0001')
expect(user.name).to eq('tester1')
expect(user.image_url).to eq('example.com/1')
end
end
end
end

0 comments on commit 3378e5d

Please sign in to comment.