Commit 30a5b12f by Hoang Phuc Do

Update RSpec for Product Item model

parent 5ef81a22
FactoryGirl.define do
factory :product_item do
quantity 1
product
order
end
end
...@@ -21,10 +21,8 @@ RSpec.describe Order, type: :model do ...@@ -21,10 +21,8 @@ RSpec.describe Order, type: :model do
user = create(:user) user = create(:user)
order = create(:order, user: user) order = create(:order, user: user)
product = create(:product, quantity: 3, price: 10) product = create(:product, quantity: 3, price: 10)
ProductItem.create(quantity: 2, create(:product_item, product: product, order: order)
product_id: product.id, expect(order.total_price).to eq 10
order: order)
expect(order.total_price).to eq 20
end end
end end
end end
...@@ -15,4 +15,18 @@ RSpec.describe ProductItem, type: :model do ...@@ -15,4 +15,18 @@ RSpec.describe ProductItem, type: :model do
it { is_expected.to belong_to(:order) } it { is_expected.to belong_to(:order) }
it { is_expected.to belong_to(:product) } it { is_expected.to belong_to(:product) }
end end
context 'custom methods' do
let!(:product) { create(:product, price: 10, quantity: 3) }
let!(:product_item) { create(:product_item, product: product) }
it 'has valid total_price' do
expect(product_item.total_price).to eq 10
end
it 'has valid update_product_quantity' do
product_item.update_product_quantity
expect(product.quantity).to eq 2
end
end
end end
...@@ -31,6 +31,10 @@ RSpec.describe Product, type: :model do ...@@ -31,6 +31,10 @@ RSpec.describe Product, type: :model do
expect(product.in_stock?(2)).to be_truthy expect(product.in_stock?(2)).to be_truthy
end end
it 'has invalid in_stock?' do
expect(product.in_stock?(4)).not_to be_truthy
end
it 'has valid belongs_to_user?' do it 'has valid belongs_to_user?' do
expect(product.belongs_to_user?(user)).not_to be_truthy expect(product.belongs_to_user?(user)).not_to be_truthy
end end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment