tap/spec/models/order_item_spec.rb

74 lines
1.7 KiB
Ruby
Raw Normal View History

2015-09-07 12:26:07 +00:00
# == Schema Information
#
# Table name: order_items
#
# id :integer not null, primary key
# order_id :integer
# product_id :integer not null
# count :integer default("0")
#
describe OrderItem do
2015-09-04 11:23:51 +00:00
it 'has a valid factory' do
order_item = create :order_item
expect(order_item).to be_valid
end
2015-09-21 14:35:54 +00:00
2015-09-21 06:50:21 +00:00
############
# FIELDS #
############
2015-09-21 06:50:21 +00:00
describe 'fields' do
2015-09-04 11:23:51 +00:00
before :each do
@order_item = create :order_item
end
2015-09-21 06:50:21 +00:00
describe 'product' do
it 'should be present' do
@order_item.product = nil
expect(@order_item).to_not be_valid
end
2015-09-04 11:23:51 +00:00
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
2015-09-21 06:50:21 +00:00
it 'should be less or equal to product stock' do
@order_item.count = @order_item.product.stock + 1
expect(@order_item).to_not be_valid
@order_item.count = @order_item.product.stock
expect(@order_item).to be_valid
end
2015-09-04 11:23:51 +00:00
end
end
2015-09-21 06:50:21 +00:00
###############
# CALLBACKS #
###############
describe 'stock change' do
2015-09-04 11:23:51 +00:00
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
2015-09-14 18:55:49 +00:00
expect{ @order_item.save }.to change{ @product.stock }.by(-@count)
2015-09-04 11:23:51 +00:00
end
2015-09-21 06:50:21 +00:00
it 'should increment on destroy' do
2015-09-04 11:23:51 +00:00
@order_item.save
2015-09-14 18:55:49 +00:00
expect{ @order_item.destroy }.to change{ @product.stock }.by(@count)
2015-09-04 11:23:51 +00:00
end
end
end