We have a requirement to talk to a third party payment provider via an http API
While I am happy with the testing of this implementation and mocking these calls where necessary, I am looking for a good way to do this in the development environment.
The interactions with the provider have significant implications for the UI that its unfeasible to test in an automated fashion.
I don't want to use VCR or mock the web requests, as this is too low level and the exact implementation has not been finalised. This doesn't mean the UI can't be tweaked and tested though, and it would be good to have an implementation that can be changed to emit the desired response by for example, changing a flag.
Here's where I am:
I have a class that proxies calls from the app to the API, and in testing its trivial to mock the calls to Acme.new
class Acme
def pay_money(amount)
AcmeAPI::PayMoneyRequest.new({
type: "Ca$h",
subscriber: @subscriber
amount: amount
})
end
end
class FakeAcme
def pay_money(amount)
canned_result = { invoice_number: "12345" }
end
end
Assuming my app uses the Acme class as such:
class PurchaseProduct
def buy_it_now
acme = Acme.new(subscriber)
result = acme.pay_money(product.price)
end
end
If I want to replace Acme with FakeAcme, what is the best way to do this?
In another programming (cough) life, I probably would have done something like:
class PurchaseProduct(acme_provider)
def buy_it_now
result = acme.pay_money
end
end
and then use some sort of DI library to specify the acme_provider at runtime.
I could do something like in the rails initializer to hack it too:
if Rails.env.development?
Acme = FakeAcme
end
Any comments/ideas on this approach or examples of alternatives would be great.
Thanks,
Steve