Skip to content

Commit 77fda2f

Browse files
committed
Add food tests
1 parent 9a55924 commit 77fda2f

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

spec/models/food_spec.rb

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
require './spec/rails_helper'
2+
3+
RSpec.describe Food, type: :model do
4+
let(:user) { User.create(name: 'JD') }
5+
subject { Food.new(name: 'Apple', measurement_unit: 'Piece', price: 1.99, quantity: 10, user:) }
6+
7+
context 'Validation' do
8+
it 'should be valid with valid attributes' do
9+
expect(subject).to be_valid
10+
end
11+
it 'should have the name present' do
12+
subject.name = nil
13+
expect(subject).to_not be_valid
14+
end
15+
it 'should have a maximum name length of 250 characters' do
16+
subject.name = 'x' * 251
17+
expect(subject).to_not be_valid
18+
end
19+
it 'should have the measurement unit present' do
20+
subject.measurement_unit = nil
21+
expect(subject).to_not be_valid
22+
end
23+
it 'should have a maximum measurement unit length of 50 characters' do
24+
subject.measurement_unit = 'x' * 51
25+
expect(subject).to_not be_valid
26+
end
27+
it 'should have the price present and greater than or equal to 0' do
28+
subject.price = nil
29+
expect(subject).to_not be_valid
30+
subject.price = -1
31+
expect(subject).to_not be_valid
32+
end
33+
it 'should have the quantity present' do
34+
subject.quantity = nil
35+
expect(subject).to_not be_valid
36+
end
37+
end
38+
context 'Associations' do
39+
it 'should belong to a user' do
40+
association = described_class.reflect_on_association(:user)
41+
expect(association.macro).to eq(:belongs_to)
42+
end
43+
it 'should have many recipe_foods' do
44+
association = described_class.reflect_on_association(:recipe_foods)
45+
expect(association.macro).to eq(:has_many)
46+
expect(association.options[:dependent]).to eq(:destroy)
47+
end
48+
end
49+
end

0 commit comments

Comments
 (0)