{"id":108,"date":"2009-07-23T18:00:44","date_gmt":"2009-07-24T00:00:44","guid":{"rendered":"http:\/\/www.pervasivecode.com\/blog\/?p=108"},"modified":"2009-07-23T18:06:59","modified_gmt":"2009-07-24T00:06:59","slug":"fancier-stubbing-of-geokit-for-rails-unit-tests","status":"publish","type":"post","link":"http:\/\/www.pervasivecode.com\/blog\/2009\/07\/23\/fancier-stubbing-of-geokit-for-rails-unit-tests\/","title":{"rendered":"Fancier Stubbing of GeoKit for Rails unit tests"},"content":{"rendered":"<p>I&#8217;m working on a Rails app that uses the ym4r_gm plugin, getting Google to do the geocoding for <a href=\"http:\/\/www.thentic.com\/\">Thentic<\/a>. I liked the idea of stubbing the web service call, because all those calls to an external service add up to over 20 seconds of test suite run time(!). That&#8217;s almost half of the 50 second run time of my unit tests (and 50 seconds is much too long for a unit test suite).<\/p>\n<p>I found a good starting point at <a href=\"http:\/\/beardendesigns.com\/blogs\/permalink\/55\">geokit stubbing for faster tests<\/a>. I also wanted a way to stub a geocoding failure, and a way to prevent any unit tests from using the real geocoding web service.<\/p>\n<p>Here&#8217;s how I did it.<br \/>\n<!--more--><br \/>\nIn test\/test_helper.rb, add this:<\/p>\n<pre>\r\nclass ActiveSupport::TestCase\r\n  def setup\r\n    GeoKit::Geocoders::MultiGeocoder.stubs(:geocode).raises(RuntimeError,\r\n      'Use mock_geocoding_success! or mock_geocoding_failure! in your test')\r\n  end\r\n\r\n  def mock_geocoding_success!\r\n    geocode_payload = GeoKit::GeoLoc.new(:lat => 123.456, :lng => 123.456)  \r\n    geocode_payload.success = true\r\n    GeoKit::Geocoders::MultiGeocoder.expects(:geocode).returns(geocode_payload)\r\n  end\r\n  \r\n  def mock_geocoding_failure!\r\n    geocode_payload = GeoKit::GeoLoc.new\r\n    geocode_payload.success = false\r\n    GeoKit::Geocoders::MultiGeocoder.expects(:geocode).returns(geocode_payload)\r\n  end\r\nend\r\n<\/pre>\n<p>What this does is to force you to choose either one of those mock_geocoding methods before you call the geocode method. To me this seems like a good idea since the integration tests that exercise the full application stack should probably be written using Cucumber and Webrat (which is what I&#8217;m using).<\/p>\n<p>You will probably want to merge my one-line setup method into your existing setup code in test_helper, if any. Also note that this uses <a href=\"http:\/\/mocha.rubyforge.org\/\">Mocha<\/a> for mocking.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m working on a Rails app that uses the ym4r_gm plugin, getting Google to do the geocoding for Thentic. I liked the idea of stubbing the web service call, because all those calls to an external service add up to over 20 seconds of test suite run time(!). That&#8217;s almost half of the 50 second &hellip; <a href=\"http:\/\/www.pervasivecode.com\/blog\/2009\/07\/23\/fancier-stubbing-of-geokit-for-rails-unit-tests\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Fancier Stubbing of GeoKit for Rails unit tests&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,26,20,15,30],"tags":[],"class_list":["post-108","post","type-post","status-publish","format-standard","hentry","category-process","category-ruby","category-ruby-on-rails","category-servers","category-testing"],"_links":{"self":[{"href":"http:\/\/www.pervasivecode.com\/blog\/wp-json\/wp\/v2\/posts\/108"}],"collection":[{"href":"http:\/\/www.pervasivecode.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.pervasivecode.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.pervasivecode.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.pervasivecode.com\/blog\/wp-json\/wp\/v2\/comments?post=108"}],"version-history":[{"count":7,"href":"http:\/\/www.pervasivecode.com\/blog\/wp-json\/wp\/v2\/posts\/108\/revisions"}],"predecessor-version":[{"id":112,"href":"http:\/\/www.pervasivecode.com\/blog\/wp-json\/wp\/v2\/posts\/108\/revisions\/112"}],"wp:attachment":[{"href":"http:\/\/www.pervasivecode.com\/blog\/wp-json\/wp\/v2\/media?parent=108"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.pervasivecode.com\/blog\/wp-json\/wp\/v2\/categories?post=108"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.pervasivecode.com\/blog\/wp-json\/wp\/v2\/tags?post=108"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}