Yesterday I was testing a Helper (module) I had written for a Rails app I am working on. I wanted to test the helper directly, and not deal with the whole controller and request/response loop just to verify some simple methods. So I started with something like the following:
class TrackingHelperTest < ActiveSupport::TestCase
include TrackingHelper
def test_record_event
...unimportant...
end
end
I ran into a problem almost immediately. Some of the methods in this Helper required access to the request object. At first I thought I went down the wrong path, and would need to test this Helper in the context of a controller. Then it hit me, mock the request up. But do I really need a whole class to store and share some instance variables? After pondering this for a few moments I ended up with something like this:
class TrackingHelperTest < ActiveSupport::TestCase
include TrackingHelper
# Mocks
MockRequest = Struct.new(:query_parameters, :path, :headers, :remote_ip, :path_parameters, :query_string)
attr_accessor :request
def setup
@request = MockRequest.new({}, "/some/location", {"HTTP_REFERER" => "somesite.com"}, "1.1.1.1", {:controller => "some", :action => "location"}, "")
end
def test_record_event
...unimportant...
end
end
Struct's are simple to use and allow for quick and easy mocking in unit tests. Try it out in your code next time you don't want to build a full class to do something simple.