Commit bd5bb37b by Hoang Phuc Do

Update RSpec for Order model

parent 07fd62c6
...@@ -43,8 +43,9 @@ class OrdersController < ApplicationController ...@@ -43,8 +43,9 @@ class OrdersController < ApplicationController
def save_order_items(order) def save_order_items(order)
@cart.product_items.values.each do |attrs| @cart.product_items.values.each do |attrs|
order.product_items.create(product: attrs[:product], product_item = order.product_items.create(product: attrs[:product],
quantity: attrs[:quantity]) quantity: attrs[:quantity])
product_item.update_product_quantity
end end
end end
......
class ProductItem < ApplicationRecord class ProductItem < ApplicationRecord
belongs_to :product, optional: true belongs_to :product, optional: true
belongs_to :order, optional: true belongs_to :order, optional: true
after_create :update_product_quantity
validates :quantity, numericality: { only_integer: true, validates :quantity, numericality: { only_integer: true,
greater_than: 0 } greater_than: 0 }
......
...@@ -15,4 +15,16 @@ RSpec.describe Order, type: :model do ...@@ -15,4 +15,16 @@ RSpec.describe Order, type: :model do
it { is_expected.to belong_to(:user) } it { is_expected.to belong_to(:user) }
it { is_expected.to have_many(:product_items) } it { is_expected.to have_many(:product_items) }
end end
context 'custom methods' do
it 'has valid total_price' do
user = create(:user)
order = create(:order, user: user)
product = create(:product, quantity: 3, price: 10)
ProductItem.create(quantity: 2,
product_id: product.id,
order: order)
expect(order.total_price).to eq 20
end
end
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