Mocked by Default, but Unmocking in Some Cases with RSpec
Uh, ya, another great blog title, but we'll get over it. We use geocoding in our app, and that's a relatively costly operation time wise, especially when you may be doing it hundreds or thousands of times when your test suite runs. I can't stub out the objects that use it in many cases, so I wanted to stub out the actual geocode call unless I truly needed real geocoding (which is only when I'm testing the actual geocoding itself, and thus is a very small part of the test suite).
spec_helper.rb file, I added:Spec::Runner.configure do |config| config.before(:each) do # Setup fake geocoding unless told not to unless @do_not_mock_geocoding fake_geocode = OpenStruct.new(:lat => 123.456, :lng => 123.456, :success => true) GeoKit::Geocoders::MultiGeocoder.stub!(:geocode).and_return(fake_geocode) end endend
@do_not_mock_geocoding variable to true. One caveat, at least from what I've found, is that you need to set that to true in a before(:all) block in your tests, so that it happens before the before(:each). This is minor, as you can just have something like:describe "with real geocoding" do before(:all) do @do_not_mock_geocoding = true end # your tests that want real geocodingend