Skip to content

Commit

Permalink
モデルスペックをリファクタ
Browse files Browse the repository at this point in the history
  • Loading branch information
djkazunoko committed Apr 10, 2024
1 parent 23651f7 commit 56103aa
Showing 1 changed file with 56 additions and 52 deletions.
108 changes: 56 additions & 52 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,55 +9,57 @@
expect(user).to be_valid
end

it 'is invalid without a provider' do
user.provider = nil
user.valid?
expect(user.errors[:provider]).to include('を入力してください')
end
describe 'validations' do
it 'is invalid without a provider' do
user.provider = nil
user.valid?
expect(user.errors[:provider]).to include('を入力してください')
end

it 'is invalid without a uid' do
user.uid = nil
user.valid?
expect(user.errors[:uid]).to include('を入力してください')
end
it 'is invalid without a uid' do
user.uid = nil
user.valid?
expect(user.errors[:uid]).to include('を入力してください')
end

it 'is invalid without a name' do
user.name = nil
user.valid?
expect(user.errors[:name]).to include('を入力してください')
end
it 'is invalid without a name' do
user.name = nil
user.valid?
expect(user.errors[:name]).to include('を入力してください')
end

it 'is invalid without an image_url' do
user.image_url = nil
user.valid?
expect(user.errors[:image_url]).to include('を入力してください')
end
it 'is invalid without an image_url' do
user.image_url = nil
user.valid?
expect(user.errors[:image_url]).to include('を入力してください')
end

it 'is invalid with a duplicate name' do
FactoryBot.create(:user)
user2 = FactoryBot.build(:user, image_url: 'example.com/2', provider: 'twitter', uid: '0002')
user2.valid?
expect(user2.errors[:name]).to include('はすでに存在します')
end
it 'is invalid with a duplicate name' do
FactoryBot.create(:user)
user2 = FactoryBot.build(:user, image_url: 'example.com/2', provider: 'twitter', uid: '0002')
user2.valid?
expect(user2.errors[:name]).to include('はすでに存在します')
end

it 'is invalid with a duplicate image_url' do
FactoryBot.create(:user)
user2 = FactoryBot.build(:user, name: 'tester2', provider: 'twitter', uid: '0002')
user2.valid?
expect(user2.errors[:image_url]).to include('はすでに存在します')
end
it 'is invalid with a duplicate image_url' do
FactoryBot.create(:user)
user2 = FactoryBot.build(:user, name: 'tester2', provider: 'twitter', uid: '0002')
user2.valid?
expect(user2.errors[:image_url]).to include('はすでに存在します')
end

it 'is invalid with a duplicate uid and provider pair' do
FactoryBot.create(:user)
user2 = FactoryBot.build(:user, name: 'tester2', image_url: 'example.com/2')
user2.valid?
expect(user2.errors[:uid]).to include('はすでに存在します')
end
it 'is invalid with a duplicate uid and provider pair' do
FactoryBot.create(:user)
user2 = FactoryBot.build(:user, name: 'tester2', image_url: 'example.com/2')
user2.valid?
expect(user2.errors[:uid]).to include('はすでに存在します')
end

it 'allows duplicate uid with different provider' do
FactoryBot.create(:user)
user2 = FactoryBot.build(:user, name: 'tester2', image_url: 'example.com/2', provider: 'twitter')
expect(user2).to be_valid
it 'allows duplicate uid with different provider' do
FactoryBot.create(:user)
user2 = FactoryBot.build(:user, name: 'tester2', image_url: 'example.com/2', provider: 'twitter')
expect(user2).to be_valid
end
end

describe '.find_or_create_from_auth_hash!' do
Expand All @@ -81,11 +83,12 @@

it 'returns the created user' do
user = described_class.find_or_create_from_auth_hash!(auth_hash)
expect(user).to be_a(described_class)
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')
expect(user).to have_attributes(
provider: 'github',
uid: '0001',
name: 'testuser',
image_url: 'https://example.com/testuser.png'
)
end
end

Expand All @@ -100,11 +103,12 @@

it 'returns the existing user' do
user = described_class.find_or_create_from_auth_hash!(auth_hash)
expect(user).to be_a(described_class)
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')
expect(user).to have_attributes(
provider: 'github',
uid: '0001',
name: 'tester1',
image_url: 'example.com/1'
)
end
end
end
Expand Down

0 comments on commit 56103aa

Please sign in to comment.