[rspec-users] cucumber - when to stub/mock

Korny Sietsma korny at sietsma.com
Mon May 4 03:11:45 EDT 2009

It's possibly worth pointing out for anyone else who needs to do
stubbing, that you don't necessarily need a framework to stub stuff; I
have cucumber tests that stub out some setup code in my app*, and they
just use monkey-patching to do the stubbing.

For instance, in the example below, you could just do:
module Geokit
  module Geocoders
    class GoogleGeocoder
      def call_geocoder_service(url)
        ... do whatever is needed to return appropriate test data

You could even use alias_method to rename the original
call_geocoder_service and call it if it got an unexpected url.

I'm not sure which is better - this at least saves you from a
dependency on an external stubbing/mocking framework.  But it could be
seen as uglier.

- Korny
* I know, this is not ideal, but it's testing a one-off migration
script, and I didn't really want to make the script setup code
generic, just to make the tests cleaner.

On Mon, May 4, 2009 at 3:42 PM, Mike Doel <mike at mikedoel.com> wrote:
> On May 4, 2009, at 1:28 AM, aslak hellesoy wrote:
>> Is there a good rule of thumb for when you make exceptions to the 'no
>> stubbing' philosophy of Cucumber?
>> This is the rule of thumb:
>> http://wiki.github.com/aslakhellesoy/cucumber/mocking-and-stubbing-with-cucumber
> I'm in a similar boat as Matt.  My app does geocoding using Andre Lewis'
> excellent geokit gem (and the Rails plugin).  That stuff hits web-based
> geocoders (Google in my case).  I also scrape other web sites.
> So, in order to make my testing executable without a net connection and
> avoid extra traffic on other folks' sites from my tests, I stub out the
> actual net call with code like this:
> def stub_geocode_lookup(address,datafile)
>  @xml = File.read(RAILS_ROOT + "/spec/fixtures/geocodes/" + datafile)
>  response = MockSuccess.new
>  response.stubs(:body).returns(@xml)
>  stub_google_call(address,response)
> end
> def stub_google_call(address,response)
>  url =
> "http://maps.google.com/maps/geo?q=#{Geokit::Inflector::url_escape(address)}&output=xml&key=#{Geokit::Geocoders::google}&oe=utf-8"
>  Geokit::Geocoders::GoogleGeocoder.stubs(:call_geocoder_service).with(url).returns(response)
> end
> I also make use of FakeWeb in a couple places to do a similar thing for the
> scraping of sites.  I have a rake task that grabs fresh versions of the
> pages I'm going to scrape and deposits it into my spec/fixtures directory.
>  If I run that every couple of days, I reduce my risk of having my fixture
> data diverge too far from what the production app actually sees.
> Mike Doel
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users

Kornelis Sietsma  korny at my surname dot com
"Every jumbled pile of person has a thinking part
that wonders what the part that isn't thinking
isn't thinking of"

More information about the rspec-users mailing list