Commit d2fa4e8c 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
......@@ -17,14 +17,13 @@ RSpec.describe Order, type: :model do
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
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
expect(order.total_price).to eq 10
end
end
end
......@@ -15,4 +15,18 @@ RSpec.describe ProductItem, type: :model do
it { is_expected.to belong_to(:order) }
it { is_expected.to belong_to(:product) }
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
......@@ -25,14 +25,22 @@ RSpec.describe Product, type: :model do
context 'custom methods' do
let!(:user) { create(:user) }
let!(:other_user) { create(:user) }
let!(:product) { create(:product, quantity: 3, user: other_user) }
let!(:product) { create(:product, quantity: 3, user: user) }
it 'has valid in_stock?' do
expect(product.in_stock?(2)).to be_truthy
end
it 'has invalid in_stock?' do
expect(product.in_stock?(4)).not_to be_truthy
end
it 'has valid belongs_to_user?' do
expect(product.belongs_to_user?(user)).not_to be_truthy
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
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