diff --git a/spec/models/list_spec.rb b/spec/models/list_spec.rb index 6ec446c7..abdeaf31 100644 --- a/spec/models/list_spec.rb +++ b/spec/models/list_spec.rb @@ -124,37 +124,52 @@ describe List do end describe '#place_order' do + subject{ list.place_order order_options } + describe "first order" do + let(:order_options){ {user: user, first_order: true} } + it 'returns an order object' do + order_options[:product_orders] = [{'product_id' => product.id, 'quantity' => 7}] + subject.should be_a Order + end - it 'returns an order object' do - list.place_order(product_orders: [{'product_id' => product.id, 'quantity' => 7}], user: user).should be_a Order - end + it 'creates an order' do + order_options[:product_orders] = [{'product_id' => product.id, 'quantity' => 7}] + expect{ subject }.to change{ Order.count }.by(1) + end - it 'creates an order' do - expect{ list.place_order(product_orders: [{'product_id' => product.id, 'quantity' => 7}], user: user) }.to change{ Order.count }.by(1) - end + describe 'broadcasting' do + it 'broadcasts to the user and the supplier the active order counter' do + # create existing order to test with counts higher than 2 + list.place_order(product_orders: [{'product_id' => product.id, 'quantity' => 7}], user: user, first_order: true) - describe 'broadcasting' do - it 'broadcasts to the user and the supplier the active order counter' do - # create existing order + # expect{ + # list.place_order(product_orders: [{product_id: product.id, quantity: 5}], user: user) + # }.to broadcast_to_user(user.id).message('orders_placed_count').with(count: 2) + + order_options[:product_orders] = [{'product_id' => product.id, 'quantity' => 7}] + expect{ subject }.to broadcast_to_supplier(supplier.id).message('new_list').with hash_including(supplier_orders_placed_count: 2) + end + end + + it 'sets the list price as kind of caching' do + product.price.should eq 2.22 list.place_order(product_orders: [{'product_id' => product.id, 'quantity' => 7}], user: user) + list.reload + list.price.should == 15.54 + end - # expect{ - # list.place_order(product_orders: [{product_id: product.id, quantity: 5}], user: user) - # }.to broadcast_to_user(user.id).message('orders_placed_count').with(count: 2) + describe 'product order creation' + end - expect{ - list.place_order(product_orders: [{'product_id' => product.id, 'quantity' => 5}], user: user, first_order: false) - }.to broadcast_to_supplier(supplier.id).message('supplier_orders_placed_count').with(count: 2) + describe "followup order" do + let(:order_options){ {user: user, first_order: false} } + describe 'broadcasting' do + it 'broadcasts to the user and the supplier the active order counter' do + order_options[:product_orders] = [{'product_id' => product.id, 'quantity' => 7}] + expect{ subject }.to broadcast_to_supplier(supplier.id).message('new_order').with hash_including(supplier_orders_placed_count: 1) + end end end - - it 'sets the list price as kind of caching' do - list.place_order(product_orders: [{'product_id' => product.id, 'quantity' => 7}], user: user) - list.reload - list.price.should == 15.54 - end - - describe 'product order creation' end describe '#close!' do diff --git a/spec/support/matchers/broadcast_to_supplier_matcher.rb b/spec/support/matchers/broadcast_to_supplier_matcher.rb index db50bc3d..7ed1f120 100644 --- a/spec/support/matchers/broadcast_to_supplier_matcher.rb +++ b/spec/support/matchers/broadcast_to_supplier_matcher.rb @@ -25,7 +25,7 @@ module Matchers relevant_broadcasts = test_broadcaster.broadcasts.select{|b| b[:channel] =~ /^\/supplier\/#{@supplier_id}/ && b[:data][:event] == @message} @failure_debug_content = "was #{relevant_broadcasts.map{|b| b[:data][:data].inspect}.join(" and ")}" - relevant_broadcasts.any?{|b| b[:data][:data] == @target_object} + relevant_broadcasts.any?{|b| @target_object === b[:data][:data] } end def message(msg)