Tuesday, January 16, 2007

Helper Module Testing

First and foremost


sudo gem install ZenTest


Then in your app/test/test_helper.rb add


require 'test/rails'


Then make a test class

class FooHelper
def print_hello_world
content_tag('p', 'Hello World!')
end
end

class FooHelperTest < Test::Rails::HelperTestCase
def test_method_goes_here
assert_equal '<p>Hello World</p>', print_hello_world
end
end


Of particular note, the name of the class that inherits from Test::Rails::HelperTestCase is very important. If the inheriting class name is MyHelperTest, make sure a module named MyHelper exists.

2 comments:

Anonymous said...
This comment has been removed by the author.
Anonymous said...

Actually, there's a better way to do this. It's in the ZenTest rdoc for Test::Rails::HelperTestCase. The idea is that you call inherited(YourModuleNameHere) at the top of your class, and it gets included. This way you can make your test calls directly without the hassle of an extra class and method proxy for each. Hope this helps! I hadn't even thought of the issue until running into your entry.