3

I have a user model with a gym association. Gyms use the google api via the geocoder gem for geocoding which is done after_save. In my gym specs, I was able to add use_vcr_cassette to have the request recorded and specs passing.

The issue now is that my user factory creates a gym association, so any spec that uses the user factory is failing because it doesn't know what to do with the geocode request since all external requests have been turned off.

How can I globally apply the previous gym cassette for the request being made by my gym factory?

Myron Marston
  • 21,452
  • 5
  • 64
  • 63
Eric M.
  • 5,399
  • 6
  • 41
  • 67
  • I have been trying to do something using the factory girl callbacks. So in the gym factory, after_build { VCR.use_cassette('Gym') }, but I keep getting undefined method arity for nil errors. – Eric M. Oct 26 '11 at 21:39

1 Answers1

0

I think there's a serious problem with your design if your model is making 3rd party API calls. You can read my longer response to a similar question here.

Community
  • 1
  • 1
Myron Marston
  • 21,452
  • 5
  • 64
  • 63
  • Very good point. I've moved the API call into an observer, where it should be. Thanks. – Eric M. Oct 27 '11 at 02:18
  • 1
    Hmm, all examples I can find show to do after_validation :geocode - I have a model which is called Location - it literally just takes a name, and address and then geocodes, it seems like it is very much what the model is meant to do? - If I take this out to an observer my Location model is very bare. Is there no way to tell FactoryGirl to always use a VCR cassette? - then I can delete the cassettes from time to time to check if the third party APIs are still alive. – Roman Gaufman Nov 15 '12 at 14:22
  • 2
    I think I agree with @RomanGaufman. If the model is responsible for bussiness logic and data persistance, then a Location model should make that 3rd api call. Every geolocation gem that I know of is designed like that... are they all wrong? I think the observer pattern is a great way to keep decoupling when data is modified by external sources (controller not involved), but the view still needs to be notified. However, I think this is not the case. I've stubbed the geocode method for now, but I would prefer to use vcr and get meaningful data from real requests. – deivid Jan 31 '13 at 12:38
  • 1
    This does not answer the question. The question is how to set a global VCR cassette, not design patterns. – Eric Terry Jul 30 '20 at 21:42