Commit 83704533 by Hoang Phuc Do

Fix merge request

parent d2fa4e8c
...@@ -10,6 +10,7 @@ class Product < ApplicationRecord ...@@ -10,6 +10,7 @@ class Product < ApplicationRecord
validates :user_id, presence: true validates :user_id, presence: true
def in_stock?(required_quantity) def in_stock?(required_quantity)
return false if required_quantity <= 0
quantity >= required_quantity.to_i quantity >= required_quantity.to_i
end end
......
...@@ -23,7 +23,7 @@ RSpec.describe Order, type: :model do ...@@ -23,7 +23,7 @@ RSpec.describe Order, type: :model do
let!(:product_item) { create(:product_item, product: product, order: order) } let!(:product_item) { create(:product_item, product: product, order: order) }
it 'has valid total_price' do it 'has valid total_price' do
expect(order.total_price).to eq 10 expect(order.total_price).to eq product.price * product_item.quantity
end end
end end
end end
...@@ -27,12 +27,19 @@ RSpec.describe Product, type: :model do ...@@ -27,12 +27,19 @@ RSpec.describe Product, type: :model do
let!(:other_user) { create(:user) } let!(:other_user) { create(:user) }
let!(:product) { create(:product, quantity: 3, user: user) } let!(:product) { create(:product, quantity: 3, user: user) }
it 'has valid in_stock?' do it 'product quantity in stock?' do
expect(product.in_stock?(2)).to be_truthy instock_quantity = 2
expect(product.in_stock?(instock_quantity)).to be_truthy
end end
it 'has invalid in_stock?' do it 'product quantity out of stock?' do
expect(product.in_stock?(4)).not_to be_truthy out_of_stock_quantity = 4
expect(product.in_stock?(out_of_stock_quantity)).not_to be_truthy
end
it 'product quantity invalid?' do
invalid_quantity = -1
expect(product.in_stock?(invalid_quantity)).not_to be_truthy
end end
it 'has valid belongs_to_user?' do it 'has valid belongs_to_user?' do
......
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