From 0581a7238d72fe3e6479e1361241d0327d71d114 Mon Sep 17 00:00:00 2001 From: benji Date: Mon, 21 Sep 2015 16:35:54 +0200 Subject: [PATCH] order test --- spec/models/order_item_spec.rb | 1 + spec/models/order_spec.rb | 62 ++++++++++++++++++++++++++++------ 2 files changed, 53 insertions(+), 10 deletions(-) diff --git a/spec/models/order_item_spec.rb b/spec/models/order_item_spec.rb index a2be719..72a2cb6 100644 --- a/spec/models/order_item_spec.rb +++ b/spec/models/order_item_spec.rb @@ -13,6 +13,7 @@ describe OrderItem do order_item = create :order_item expect(order_item).to be_valid end + ############ # FIELDS # ############ diff --git a/spec/models/order_spec.rb b/spec/models/order_spec.rb index 380541f..373471c 100644 --- a/spec/models/order_spec.rb +++ b/spec/models/order_spec.rb @@ -20,16 +20,58 @@ describe Order do expect(@order).to be_valid end - describe 'price' do - it 'should be calculated from order_items' do - @order = build :order, products_count: 0 - sum = (create_list :product, 1 + rand(10)).map do |p| - create(:order_item, order: @order, product: p, count: 1 + rand(5)) do |oi| - @order.order_items << oi - end - end.map{ |oi| oi.count * oi.product.price_cents }.sum - @order.valid? - expect(@order.price_cents).to eq(sum) + ############ + # FIELDS # + ############ + + describe 'fields' do + describe 'user' do + it { Order.reflect_on_association(:user).macro.should eq(:belongs_to) } + it 'should be present' do + @order.user = nil + expect(@order).to_not be_valid + end + end + + describe 'price_cents' do + it 'should be calculated from order_items' do + @order = build :order, products_count: 0 + sum = (create_list :product, 1 + rand(10)).map do |p| + create(:order_item, order: @order, product: p, count: 1 + rand(5)) do |oi| + @order.order_items << oi + end + end.map{ |oi| oi.count * oi.product.price_cents }.sum + @order.save + expect(@order.price_cents).to eq(sum) + end + end + + describe 'order_items' do + it 'should be validated' do + @order.order_items << OrderItem.new(count: -5) + expect(@order).to_not be_valid + end + end + + describe 'products' do + it 'should be present' do + @order.products.clear + expect(@order).to_not be_valid + end end end + + ############### + # CALLBACKS # + ############### + + describe 'empty order_items' do + it 'should be removed' do + product = create :product + @order.order_items << create(:order_item, order: @order, product: product, count: 0) + @order.save + expect(@order.order_items.where(product: product)).to be_empty + end + end + end