Search code examples
ruby-on-railsrubyrecurly

How to test Recurly in Ruby, possibly using VCR


I have a rails application that uses Recurly for its transactions. I am trying to write automated tests for some of the helper functions that I have written.

A super simple example of a function...

def status_for_display
   transaction.status.capitalize
end

In order to test these functions, I need to have a Recurly::Account object as well as associated Recurly::Transaction objects.

I have tried going the route of using Recurly::Account.create & Recurly::Transaction.create but I cannot seem to get the transactions to match up with the account.

I am also wondering if it doesn't just make better sense to use the VCR gem to make this happen. In which case, how would I go about doing that? I've never really managed to get VCR setup properly.


Solution

  • VCR is, by in large, plug and play. Once you have it configured and enabled, it'll intercept all HTTP requests and try to play back the data from a cassette. The problem with VCR, though, is that it is request data specific. In order for it to work right, you need to ensure that your tests are always sending the exact same request params to Recurly. You can work around this by having it skip certain things, but it's generally a pain.

    The other option is to just use something like Webmock directly and house your own "known responses" for your Recurly calls, but then it's up to you to ensure that you responses stay in sync with the API.

    In the end, I'd probably recommend going the VCR route but structuring your tests such that you have known good and bad test scenarios so you can get the real benefits of the cassettes.