Last active
December 19, 2015 23:09
-
-
Save jetaggart/6033046 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class PaymentProcessor | |
| def initlialize(stripe = Stripe.new(api_key: 'whatever')) | |
| self.stripe = stripe | |
| end | |
| def void_transaction(transaction) | |
| stripe.void_transaction(transaction) | |
| end | |
| def process(*payment_attributes) | |
| stripe.process(payment_attributes) | |
| end | |
| private | |
| attr_reader stripe | |
| end | |
| class SuccessPaymentProcessorDouble | |
| def process(*_) | |
| double(Stripe::Transaction, success?: true) | |
| end | |
| def void_transaction(_) | |
| true | |
| end | |
| end | |
| class FailurePaymentProcessorDouble | |
| def process(*_) | |
| raise Stripe::PaymentFailed | |
| end | |
| def void_transaction() | |
| raise Stripe::TransactionVoidFailed | |
| end | |
| end | |
| ### in my real application | |
| payment_processor = PaymentProcessor.new | |
| ### in my happy path test code | |
| payment_processor = SuccessPaymentProcessorDouble.new | |
| payment_processor.process # yay! | |
| ### in my fail test code | |
| payment_processor = FailurePaymentProcessorDouble.new | |
| payment_processor.process # oh no! | |
| #### you could also inject a bad stripe object to simulate failures | |
| bad_stripe = double(Stripe) | |
| bad_stripe.stub(:process).and_raise(TimeoutError) | |
| payment_processor = PaymentProcessor.new(bad_stripe) | |
| payment_processor.process # boom! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment