Skip to content

Commit

Permalink
Add model tests for user_rspec
Browse files Browse the repository at this point in the history
  • Loading branch information
Estete9 committed Feb 16, 2024
1 parent 15cc0ff commit 6b7300c
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
require 'rails_helper'

RSpec.describe User, type: :model do
describe 'validations' do
it { should validate_presence_of(:username) }
it { should validate_presence_of(:name) }
it { should validate_presence_of(:email) }
it { should validate_length_of(:password).is_at_least(8) }
it { should allow_value('username').for(:username) }
it { should_not allow_value('user name').for(:username) }
it { should_not allow_value('user@name').for(:username) }
it { should_not allow_value('user name!').for(:username) }
end

describe 'associations' do
it { should have_many(:reservations).dependent(:destroy) }
end

describe 'enums' do
it { should define_enum_for(:role).with_values(user: 0, admin: 1) }
end

describe 'after_initialize' do
it 'sets the default role to user' do
user = User.new
expect(user.role).to eq('user')
end
end

describe 'methods' do
describe '#admin?' do
it 'returns true if the user has admin role' do
admin_user = FactoryBot.create(:user, :admin)
expect(admin_user.admin?).to be_truthy
end

it 'returns false if the user does not have admin role' do
user = FactoryBot.create(:user)
expect(user.admin?).to be_falsey
end
end
end
end

0 comments on commit 6b7300c

Please sign in to comment.