Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve spec for Spree::Order#finalize! #12662

Merged
merged 5 commits into from
Jul 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 21 additions & 20 deletions spec/models/spree/order_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -216,35 +216,36 @@
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice way to test this. I probably would have gone down the unnecessary task of freezing time and checking it changed to the current time..

end

it "should sell inventory units" do
order.shipments.each do |shipment|
expect(shipment).to receive(:update!)
expect(shipment).to receive(:finalize!)
end
Comment on lines -227 to -230
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the typo in the commit message. order.shipments was empty.

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

Expand Down
Loading