From 758e75f993cc858134f657b212b4e590de01074c Mon Sep 17 00:00:00 2001 From: Yusuf Sholotan Date: Wed, 18 Oct 2023 04:32:44 +0100 Subject: [PATCH] Add unit tests --- spec/models/food_spec.rb | 49 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 spec/models/food_spec.rb diff --git a/spec/models/food_spec.rb b/spec/models/food_spec.rb new file mode 100644 index 0000000..198e47d --- /dev/null +++ b/spec/models/food_spec.rb @@ -0,0 +1,49 @@ +require 'rails_helper' + +RSpec.describe Food, type: :model do + let(:food) { Food.new(name: 'Apple', measurement_unit: 'lb') } + + it 'validates the presence of the name' do + food.name = nil + expect(food).to_not be_valid + end + + it 'validates the presence of the measurement unit' do + food.measurement_unit = nil + expect(food).to_not be_valid + end + + it 'combines the name and measurement unit' do + expect(food.name_with_measurement_unit).to eq 'Apple (lb)' + end + + it 'validates the minimum name length of 2 characters' do + food.name = 'Y' + expect(food).to_not be_valid + end + + it 'validates the maximum name length of 50 characters' do + food.name = 'a' * 51 + expect(food).to_not be_valid + end + + it 'validates the presence of a price' do + food.price = nil + expect(food).to_not be_valid + end + + it 'validates a non-negative price' do + food.price = -1 + expect(food).to_not be_valid + end + + it 'validates the presence of a quantity' do + food.quantity = nil + expect(food).to_not be_valid + end + + it 'validates a non-negative quantity' do + food.quantity = -1 + expect(food).to_not be_valid + end +end