Testing ApplicationHelper
Monday, January 28th, 2008We wanted to test our ApplicationHelper code. Unfortunately the obvious approach doesn’t quite work out-of-the-box in Rails 1.2. With the help of various blog postings (The Official Documentation Source of Rails) we figured out an answer: in test/unit/application_helper_test.rb, say something like:
require File.dirname(__FILE__) + '/../test_helper'
class ApplicationHelperTest < Test::Unit::TestCase
include ApplicationHelper
include ERB::Util
def test_something
...
end
end
Including ApplicationHelper lets you call AH methods in tests directly. Since AH is a module that always gets mixed in to classes that provide ERB, you need to include ERB::Util if you have any helpers that call methods like h().Now you can tell me The Easy Way, or why this is obvious to any Real Programmer, or whatever. If it helps one poor schmuck trying to figure out Rails, that’s good enough for me.
