Skip to content

Instantly share code, notes, and snippets.

@indrekj
Created April 2, 2013 12:31
Show Gist options
  • Select an option

  • Save indrekj/5291860 to your computer and use it in GitHub Desktop.

Select an option

Save indrekj/5291860 to your computer and use it in GitHub Desktop.
class MakesBookingPayment
def initialize(booking, payment_details)
@booking = booking
@payment_details = payment_details
end
def pay!
response = process_payment
Transaction.record_purchase(@booking, response)
if response.success?
Payment.create_payment(@booking)
else
false
end
end
private
def process_payment
PaymentProcessor.process(@booking, @payment_details)
end
end
require "minitest/autorun"
require "minitest/pride"
require "mocha/setup"
require "virtus"
require_relative "./makes_booking_payment"
# I'd probably use an actual Booking class if it is simple value object.
class Booking
include Virtus
attribute :balance, Integer
end
# I'm also okay with dependency injection, but here it seems easier to
# just stub these classes. When you actually need different payment
# processor, then definitely use DI.
class Transaction; end
class PaymentProcessor; end
class Payment; end
describe MakesBookingPayment do
# Setup variables for successful path
let(:booking) { Booking.new(balance: 2.34) }
let(:payment_details) { mock("Payment details") }
let(:response) { stub("Payment response", success?: true) }
let(:action) { MakesBookingPayment.new(booking, payment_details) }
before do
Payment.stubs(:create_payment).returns(true)
Transaction.stubs(:record_purchase)
PaymentProcessor.stubs(:process).returns(response)
end
it "processes the booking using payment processor" do
PaymentProcessor.
expects(:process).with(booking, payment_details).returns(response)
action.pay!
end
it "keeps a record of each transaction" do
Transaction.expects(:record_purchase).with(booking, response)
action.pay!
end
it "creates a payment record when successful transaction" do
Payment.expects(:create_payment).with(booking)
action.pay!
end
it "returns false when processing payment fails" do
response.stubs(:success?).returns(false)
action.pay!.must_equal false
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment