Testing Helpers in Rails
Posted by Peter Donald Wed, 13 Jun 2007 14:01:00 GMT
It can be useful at times to unit test helpers to make sure they generate correct html. It is not obvious how to do this at first. So far I have been testing my helper by defining a class “MyClass” at the top of my unit test and including all the appropriate modules. I also need to define a url_for method if I ever want to test helpers that generate links.
The code follows (Replace MyHelper with your appropriate helper class);
class MyClass
include ERB::Util
include ActionView::Helpers::TagHelper
include ActionView::Helpers::UrlHelper
include MyHelper
def url_for(options)
ActionController::Routing::Routes.reload if ActionController::Routing::Routes.empty?
generated_path, extra_keys = ActionController::Routing::Routes.generate_extras(options, {})
generated_path
end
endThen in my tests I do something like;
def test_revision_link
assert_equal(
"<a href=\"http://svn.sourceforge.net/viewvc/jikesrvm?view=rev&revision=22\">22</a>",
MyClass.new.revision_link(22))
endSeems easy enough to do in retrospect but things usually do.
The ZenTest gem from Ryan Davis and Eric Hodel adds support for testing Rails helpers. That aspect is not well documented, but it’s in there.
Luke Melia
Tons of errors in eyelook: look for access_denied!, find_active_user and check sqlite compatibility (either drop foreign key constraints (nearly useless in rails either) or add support to active_record_extensions. Layout looks awfull in Safari 3.0 And is a permalink self-explanatory?
Michael
Looks like I should have explicitly set the version in the raaa plugin as it recently changed and that is what broke eyelook. As for the rest – well true it may look ugly but I moved my photos to smugmug (see http://www.realityforge.org/articles/2006/07/03/moving-from-eyelook-to-smugmug)
Peter Donald