Add tests for order_items

This commit is contained in:
benji 2015-09-04 13:23:51 +02:00
parent 168b723dce
commit 43fd368bc7
2 changed files with 42 additions and 5 deletions

View file

@ -13,7 +13,7 @@ class OrderItem < ActiveRecord::Base
belongs_to :product
validates :product, presence: true
validates :count, numericality: { only_integer: true, greater_than_or_equal_to: 0 }
validates :count, presence: true, numericality: { only_integer: true, greater_than_or_equal_to: 0 }
after_create :remove_from_stock

View file

@ -1,9 +1,46 @@
describe OrderItem do
before :each do
@order_item = create :order_item
it 'has a valid factory' do
order_item = create :order_item
expect(order_item).to be_valid
end
it 'has a valid factory' do
expect(@order_item).to be_valid
describe 'validations' do
before :each do
@order_item = create :order_item
end
it 'product should be present' do
@order_item.product = nil
expect(@order_item).to_not be_valid
end
describe 'count' do
it 'should be present' do
@order_item.count = nil
expect(@order_item).to_not be_valid
end
it 'should be positive' do
@order_item.count = -5
expect(@order_item).to_not be_valid
end
end
end
describe 'product stock' do
before :each do
@product = create :product
@count = rand 10
@order_item = build :order_item, product: @product, count: @count
end
it 'should decrement on create' do
expect{@order_item.save}.to change{@product.stock}.by(-@count)
end
it 'should increment on cancel' do
@order_item.save
expect{@order_item.cancel}.to change{@product.stock}.by(@count)
end
end
end