diff --git a/Gemfile b/Gemfile index 0eeac45..b68eaff 100644 --- a/Gemfile +++ b/Gemfile @@ -58,7 +58,6 @@ group :test do gem 'shoulda-matchers' end - gem 'devise' gem 'devise-jwt' gem 'jsonapi-serializer' diff --git a/spec/models/car_detail_spec.rb b/spec/models/car_detail_spec.rb index 77f9dee..d4fe746 100644 --- a/spec/models/car_detail_spec.rb +++ b/spec/models/car_detail_spec.rb @@ -4,13 +4,13 @@ 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, @@ -18,7 +18,6 @@ torque: nil, fuel_economy: nil, seating_capacity: nil, - seating_capacity: nil, cargo_space: nil, infotainment_system: nil, safety_rating: nil, diff --git a/spec/models/car_spec.rb b/spec/models/car_spec.rb index dd2396e..6d6d8f7 100644 --- a/spec/models/car_spec.rb +++ b/spec/models/car_spec.rb @@ -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 @@ -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 diff --git a/spec/models/city_spec.rb b/spec/models/city_spec.rb index 819b57e..46cb31d 100644 --- a/spec/models/city_spec.rb +++ b/spec/models/city_spec.rb @@ -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 @@ -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 diff --git a/spec/models/engine_type_spec.rb b/spec/models/engine_type_spec.rb index f740504..30bfcb9 100644 --- a/spec/models/engine_type_spec.rb +++ b/spec/models/engine_type_spec.rb @@ -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 diff --git a/spec/models/reservation_spec.rb b/spec/models/reservation_spec.rb index 978609c..38530fe 100644 --- a/spec/models/reservation_spec.rb +++ b/spec/models/reservation_spec.rb @@ -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) @@ -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) } diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb new file mode 100644 index 0000000..e24fcde --- /dev/null +++ b/spec/models/user_spec.rb @@ -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 diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index 232c774..505f3f1 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -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