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 e055241
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 36 deletions.
1 change: 0 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ group :test do
gem 'shoulda-matchers'
end


gem 'devise'
gem 'devise-jwt'
gem 'jsonapi-serializer'
7 changes: 3 additions & 4 deletions spec/models/car_detail_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,20 @@
let(:car) { FactoryBot.create(:car) }
let(:engine_type) { FactoryBot.create(:engine_type) }

let(:car_detail) { FactoryBot.create(:car_detail, car: car, engine_type: engine_type) }
let(:car_detail) { FactoryBot.create(:car_detail, car:, engine_type:) }

it "Creates a reservation with valid attributes" do
it 'Creates a reservation with valid attributes' do
expect(car_detail).to be_valid
end

it "is invalid withinvalid attributes" do
it 'is invalid withinvalid attributes' do
car_detail = FactoryBot.build(
:car_detail,
horsepower: nil,
car: nil,
torque: nil,
fuel_economy: nil,
seating_capacity: nil,
seating_capacity: nil,
cargo_space: nil,
infotainment_system: nil,
safety_rating: nil,
Expand Down
20 changes: 10 additions & 10 deletions spec/models/car_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@
RSpec.describe Car, type: :model do
let(:car) { FactoryBot.create(:car) }

it "is valid with a name" do
it 'is valid with a name' do
expect(car).to be_valid
end

it "is invalid without a name" do
it 'is invalid without a name' do
car = FactoryBot.build(:car, name: nil)

expect(car).to_not be_valid
end

it "is invalid when the name is too short" do
car = FactoryBot.build(:car, name: "V")
it 'is invalid when the name is too short' do
car = FactoryBot.build(:car, name: 'V')

expect(car).to_not be_valid
end

it "is invalid when the name is too long" do
it 'is invalid when the name is too long' do
car = FactoryBot.build(:car, name: "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus sit amet
massa vel nulla eleifend semper sed vel est. Phasellus nulla metus, molestie vel condimentum sit amet, commodo sed
libero. Sed massa tellus, auctor sed varius vitae, egestas nec felis. Curabitur sit amet laoreet turpis, non
Expand All @@ -34,19 +34,19 @@
expect(car).to_not be_valid
end

it "can have many reservations" do
it 'can have many reservations' do
engine_type = FactoryBot.create(:engine_type)
car_detail = FactoryBot.create(:car_detail, engine_type: engine_type, car: car)
FactoryBot.create(:car_detail, engine_type:, car:)
user = FactoryBot.create(:user)
city = FactoryBot.create(:city)

reservation1 = FactoryBot.create(:reservation, car: car, city: city, user: user)
reservation2 = FactoryBot.create(:reservation, car: car, city: city, user: user)
reservation1 = FactoryBot.create(:reservation, car:, city:, user:)
reservation2 = FactoryBot.create(:reservation, car:, city:, user:)

expect(car.reservations).to include(reservation1, reservation2)
end

it "accepts nested attributes for car_detail" do
it 'accepts nested attributes for car_detail' do
should accept_nested_attributes_for(:car_detail)
end
end
20 changes: 10 additions & 10 deletions spec/models/city_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@
RSpec.describe City, type: :model do
let(:city) { FactoryBot.create(:city) }

it "Creates a city with valid attributes" do
it 'Creates a city with valid attributes' do
expect(city).to be_valid
end

describe "fails to create a city with invalid attributes" do
it "Name attribute is not present" do
describe 'fails to create a city with invalid attributes' do
it 'Name attribute is not present' do
city = FactoryBot.build(:city, name: nil)

expect(city).to_not be_valid
end

it "name attribute is too long" do
it 'name attribute is too long' do
city = FactoryBot.build(:city, name: "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut sed sem lectus.
Ut non fermentum velit. Nunc facilisis diam id ante auctor rutrum. Proin mauris ipsum, scelerisque et convallis
ac, molestie quis turpis. Vivamus rhoncus ex non arcu mattis, non sollicitudin est pellentesque. Curabitur neque
Expand All @@ -28,21 +28,21 @@
expect(city).to_not be_valid
end

it "name attribute is too short" do
city = FactoryBot.build(:city, name: "")
it 'name attribute is too short' do
city = FactoryBot.build(:city, name: '')

expect(city).to_not be_valid
end
end

it "It can have many reservation records" do
it 'It can have many reservation records' do
engine_type = FactoryBot.create(:engine_type)
car = FactoryBot.create(:car)
car_detail = FactoryBot.create(:car_detail, engine_type: engine_type, car: car)
FactoryBot.create(:car_detail, engine_type:, car:)
user = FactoryBot.create(:user)

reservation1 = FactoryBot.create(:reservation, car: car, city: city, user: user)
reservation2 = FactoryBot.create(:reservation, car: car, city: city, user: user)
reservation1 = FactoryBot.create(:reservation, car:, city:, user:)
reservation2 = FactoryBot.create(:reservation, car:, city:, user:)

expect(city.reservations).to include(reservation1, reservation2)
end
Expand Down
10 changes: 5 additions & 5 deletions spec/models/engine_type_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@
RSpec.describe EngineType, type: :model do
let(:engine_type) { FactoryBot.create(:engine_type) }

it "is valid with a name" do
it 'is valid with a name' do
expect(engine_type.name).to eq('MyString')
expect(engine_type).to be_valid
end

it "is invalid without a name" do
it 'is invalid without a name' do
engine_type = FactoryBot.build(:engine_type, name: nil)

expect(engine_type.name).to eq(nil)
expect(engine_type).to_not be_valid
end

it "has can have many car details" do
it 'has can have many car details' do
engine_type = FactoryBot.create(:engine_type)
car = FactoryBot.create(:car)
car_detail1 = FactoryBot.create(:car_detail, car: car, engine_type: engine_type)
car_detail2 = FactoryBot.create(:car_detail, car: car, engine_type: engine_type)
car_detail1 = FactoryBot.create(:car_detail, car:, engine_type:)
car_detail2 = FactoryBot.create(:car_detail, car:, engine_type:)

expect(engine_type.car_details).to include(car_detail1, car_detail2)
end
Expand Down
8 changes: 4 additions & 4 deletions spec/models/reservation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
let(:city) { FactoryBot.create(:city) }
let(:car) { FactoryBot.create(:car) }
let(:user) { FactoryBot.create(:user) }
let(:reservation) { FactoryBot.create(:reservation, car: car, city: city, user: user) }
let(:reservation) { FactoryBot.create(:reservation, car:, city:, user:) }

it "Creates a reservation with valid attributes" do
it 'Creates a reservation with valid attributes' do
expect(reservation).to be_valid
end

describe "with invalid attributes" do
describe 'with invalid attributes' do
it "Doesn't create a reservation with invalid car attribute" do
reservation = FactoryBot.build(:reservation, car: nil)

Expand All @@ -30,7 +30,7 @@
end
end

describe "associations" do
describe 'associations' do
it { should belong_to(:city) }
it { should belong_to(:car) }
it { should belong_to(:user) }
Expand Down
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
3 changes: 1 addition & 2 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@
abort e.to_s.strip
end
RSpec.configure do |config|

# configurations for Shoulda Matchers
# configurations for Shoulda Matchers
config.include FactoryBot::Syntax::Methods
config.include Shoulda::Matchers::ActiveModel, type: :model
config.include Shoulda::Matchers::ActiveRecord, type: :model
Expand Down

0 comments on commit e055241

Please sign in to comment.