2015-09-07 14:26:07 +02: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")
|
|
|
|
#
|
|
|
|
|
2015-09-02 20:33:35 +02:00
|
|
|
describe OrderItem do
|
2015-09-04 13:23:51 +02:00
|
|
|
it 'has a valid factory' do
|
|
|
|
order_item = create :order_item
|
|
|
|
expect(order_item).to be_valid
|
2015-09-02 20:33:35 +02:00
|
|
|
end
|
|
|
|
|
2015-09-04 13:23:51 +02:00
|
|
|
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
|
2015-09-04 16:47:49 +02:00
|
|
|
|
|
|
|
it 'should not be 0' do
|
|
|
|
@order_item.count = 0
|
|
|
|
expect(@order_item).to_not be_valid
|
|
|
|
end
|
2015-09-04 13:23:51 +02:00
|
|
|
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
|
2015-09-02 20:33:35 +02:00
|
|
|
end
|
|
|
|
end
|