Commit 03678484 by Trong Huu Nguyen

Merge branch 'dhp_rspec_3' into 'development'

[REVIEW] Update RSpec for model

See merge request !11
parents 07fd62c6 eabf482e
...@@ -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
......
...@@ -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
......
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 }
......
FactoryGirl.define do
factory :product_item do
quantity 1
product
order
end
end
...@@ -15,4 +15,15 @@ RSpec.describe Order, type: :model do ...@@ -15,4 +15,15 @@ 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
let(:user) { create(:user) }
let!(:order) { create(:order, user: user) }
let!(:product) { create(:product, quantity: 3, price: 10) }
let!(:product_item) { create(:product_item, product: product, order: order) }
it 'has valid total_price' do
expect(order.total_price).to eq product.price * product_item.quantity
end
end
end end
...@@ -15,4 +15,19 @@ RSpec.describe ProductItem, type: :model do ...@@ -15,4 +15,19 @@ 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) }
let!(:current_product_quantity) { product.quantity }
it 'has valid total_price' do
expect(product_item.total_price).to eq product.price * product_item.quantity
end
it 'has valid update_product_quantity' do
product_item.update_product_quantity
expect(product.quantity).to eq current_product_quantity - product_item.quantity
end
end
end end
...@@ -21,4 +21,33 @@ RSpec.describe Product, type: :model do ...@@ -21,4 +21,33 @@ RSpec.describe Product, type: :model do
it { is_expected.to belong_to(:user) } it { is_expected.to belong_to(:user) }
it { is_expected.to belong_to(:category) } it { is_expected.to belong_to(:category) }
end end
context 'custom methods' do
let!(:user) { create(:user) }
let!(:other_user) { create(:user) }
let!(:product) { create(:product, quantity: 3, user: user) }
it 'product quantity in stock?' do
instock_quantity = 2
expect(product.in_stock?(instock_quantity)).to be_truthy
end
it 'product quantity out of stock?' do
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
it 'has valid belongs_to_user?' do
expect(product.belongs_to_user?(user)).to be_truthy
end
it 'has invalid belongs_to_user?' do
expect(product.belongs_to_user?(other_user)).not_to be_truthy
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