Merge pull request #12662 from mkllnk/order-finalise-spec

Improve spec for Spree::Order#finalize!
This commit is contained in:
David Cook
2024-07-15 11:53:57 +10:00
committed by GitHub

View File

@@ -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