Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>As the author of the VCR gem, you'd probably expect me to recommend it for cases like these. I do indeed recommend it for testing HTTP-dependent code, but I think there's an underlying problem with your design. Don't forget that TDD (test-driven development) is meant to be a design discipline, and when you find it painful to easily test something, that's telling you something about your design. Listen to your tests' pain!</p> <p>In this case, I think your model has no business making the 3rd party API call. It's a pretty significant violation of the single responsibility principle. Models should be responsible for the validation and persistence of some data, but this is definitely beyond that.</p> <p>Instead, I would recommend you move the 3rd party API call into an observer. Pat Maddox has a <a href="http://patmaddox.com/blog/better-rails-testing-decoupling-observers.html" rel="nofollow">great blog post</a> discussing how observers can (and should) be used to loosely couple things without violating the SRP (single responsibility principle), and how that makes testing, much, much easier, and also improves your design.</p> <p>Once you've moved that into an observer, it's easy enough to disable the observer in your unit tests (except for the specific tests for that observer), but keep it enabled in production and in your integration tests. You can use Pat's <a href="https://github.com/patmaddox/no-peeping-toms" rel="nofollow">no-peeping-toms</a> plugin to help with this, or, if you're on rails 3.1, you should check out the <a href="https://github.com/rails/rails/blob/v3.1.0/activemodel/lib/active_model/observer_array.rb#L18-39" rel="nofollow">new functionality</a> built in to ActiveModel that allows you to <a href="https://github.com/rails/rails/blob/v3.1.0/activemodel/lib/active_model/observer_array.rb#L18-39" rel="nofollow">easily enable/disable observers</a>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload