diff --git a/spec/models/spree/order_spec.rb b/spec/models/spree/order_spec.rb index 50ef24dc72..74d384e2c8 100644 --- a/spec/models/spree/order_spec.rb +++ b/spec/models/spree/order_spec.rb @@ -216,35 +216,36 @@ RSpec.describe Spree::Order do end end - context "#finalize!" do - let(:order) { Spree::Order.create } + describe "#finalize!" do + subject(:order) { Spree::Order.create } + it "should set completed_at" do - expect(order).to receive(:touch).with(:completed_at) - order.finalize! + expect { + order.finalize! + order.reload + }.to change { + order.completed_at + }.from(nil) end - it "should sell inventory units" do - order.shipments.each do |shipment| - expect(shipment).to receive(:update!) - expect(shipment).to receive(:finalize!) - end - order.finalize! - end + it "updates shipments and decreases stock" do + order = create(:order_ready_for_confirmation) + shipment = order.shipments.first + shipment.update_columns(updated_at: 1.minute.ago) - it "should decrease the stock for each variant in the shipment" do - order.shipments.each do |shipment| - expect(shipment.stock_location).to receive(:decrease_stock_for_variant) - end - order.finalize! + expect { + order.finalize! + }.to change { order.variants.first.on_hand }.by(-1) + .and change { shipment.updated_at } end it "should change the shipment state to ready if order is paid" do - Spree::Shipment.create(order:) - order.shipments.reload + order = create(:order_ready_for_confirmation) - allow(order).to receive_messages(paid?: true, complete?: true) - order.finalize! + order.payments.first.capture! + order.next! # calls `finalize!` order.reload # reload so we're sure the changes are persisted + expect(order.shipment_state).to eq 'ready' end