Inserting a VCR cassette in a factory?

744 views
Skip to first unread message

ralx...@gmail.com

unread,
Mar 10, 2013, 12:34:46 AM3/10/13
to vcr-...@googlegroups.com
I have a Subscription model that makes a 3rd party API call to Stripe in an after_save.

I've read posts and heard opinions that making 3rd party API calls in a model is a bad idea/design flaw, and it should be moved to an observer. However, this is a business critical component of the model so I think it is smart to keep it attached to the model. If the call to Stripe fails or throws an error, I want to rollback the transaction immediately and show the user the error to fix, which is most likely incorrectly entered credit card info.

So every time my factory creates a Subscription, it makes an API call. My question is: Is there any way I can use VCR to define a cassette IN the factory? So every time I call FactoryGirl.create(:subscription), VCR knows to use a cassette for those resulting API calls to Stripe.

If it's not possible, do you have any tips on how I should better design the logic of the app?

Myron Marston

unread,
Mar 10, 2013, 11:32:16 PM3/10/13
to VCR Rubygem
Two ideas:

* Instantiate your factory objects via a method you define, that can
wrap the `FactoryGirl.create` (or whatever) call in a VCR cassette.
* Use VCR's request hooks to automatically insert and eject a cassette
for certain HTTP requests:

https://relishapp.com/vcr/vcr/v/2-4-0/docs/hooks/around-http-request-hook!

HTH,
Myron

apen...@gmail.com

unread,
Nov 5, 2014, 9:39:03 AM11/5/14
to vcr-...@googlegroups.com, ralx...@gmail.com
Hey I'm having the exact same issue... how did you solve it?
Reply all
Reply to author
Forward
0 new messages