The other day I found myself needing to test a class (this seems to happen a lot), but I ran into a sticky situation. I wanted to test the validity of an instance method, easy enough, and then later wanted to test another instance method that relies on the first. The problem lies in the fact that the first relies on an external service that doesn’t provide stable data, making it difficult, if not impossible to test.
A little background on the code you’re about to see. It is quite common that I must consume XML from internal services, but I have no way of knowing how much data will be returned with each request (as these feeds rely on other services). Downloading tons of XML and iterating over it (and performing actions based on the data) is slow, cumbersome and can cause annoying situations if it crashes in the middle. To help with scalability (the buzzword of the year!) the XML feed listens for 2 query string parameters, start_event and limit. Knowing this I built a small iterator object to wrap a Hpricot instance and fetch XML with only 100 rows, and to repeat until it has reached the end.
Boring business logic and methods have been removed from the following sample.
class XmlIterator
include Enumerable
...
def each
while data = get_xml(build_url)
results = (data/@search_term)
results.each{|x| yield x}
@start_event = (data/'activations').first['end_event']
break if results.length < @limit
end
end
private
def build_url
query_string = "start_event=#{@start_event}&limit=#{@limit}"
return "#{@path}?#{query_string}"
end
...
end
As you can see, I've built my own enumerable object, but the each method is really a proxy back to a Hpricot doc object (produced in the get_xml method I have conveniently removed). It will fetch 100 rows, process them, and then fetch 100 more, and continue until the result set is returning less than 100, meaning it was the last "page" of data.
Now for the testing. Validating build_url was pretty simple, set some instance variables, call the method and compare the returned string. But what about each? I don't want to hit the service to test the each method, it would really be great if I could have it look at a local file in my mocks directory. All I really care to test here is that the proxying of the iteration works as expected. I struggled with how to do this for a few hours.
My initial reaction was to mock up the HTTP service using something like WebBrick, but that sounded like a lot of work, and a maintenance nightmare in the future. I knew the solution was to change the build_url method, but how can I do that without overriding it completely, as I need to test the original implementation. This meant placing a file in test/mocks/test was out.
Then it hit me, why not just change the build_url for that one test. There are 2 ways to go about this (that I know of). Both can be seen in the example below:
require File.dirname(__FILE__) + '/../test_helper'
class XmlIteratorTest < ActiveSupport::TestCase
def setup
@iterator = XmlIterator.new("http://www.madeup.com")
end
def test_each
def @iterator.build_url
"#{RAILS_ROOT}/test/mocks/test/activations.xml"
end
...
end
def test_each_again
@iterator.instance_eval do
def build_url
"#{RAILS_ROOT}/test/mocks/test/activations.xml"
end
end
...
end
end
Both @iterator.instance_eval and def @iterator.build_url will produce the same result, a new version of build_url for that specific instance of @iterator, it's just a matter of preference which you chose to do. Personally, since it's just one method I'm redefining I like the first version, but if I was overriding 2 or more I'd probably go with the second.