require "test_helper" class OrderEmailTest < ActiveSupport::TestCase def setup @order = orders(:draft_order) end test "sends purchase confirmation email when order is marked as paid" do # Mock the mailer to capture the call TicketMailer.expects(:purchase_confirmation_order).with(@order).returns(stub(deliver_now: true)) @order.mark_as_paid! assert_equal "paid", @order.status end test "activates all tickets when order is marked as paid" do @order.tickets.update_all(status: "reserved") # Mock the mailer to avoid actual email sending TicketMailer.stubs(:purchase_confirmation_order).returns(stub(deliver_now: true)) @order.mark_as_paid! assert @order.tickets.all? { |ticket| ticket.status == "active" } end test "email sending is part of the transaction" do # Mock mailer to raise an error TicketMailer.stubs(:purchase_confirmation_order).raises(StandardError.new("Email error")) assert_raises(StandardError) do @order.mark_as_paid! end # Order should not be marked as paid if email fails assert_equal "draft", @order.reload.status end end