Friday, November 23, 2007

Testing ANY helper in a functional test

Recipe #44 - "Write Tests for Your Helpers" in the book "Rails recipes" instructs us doing this by including helper modules in subclasses of Test::Unit::TestCase. However it wouldn't work if a helper you want to test is implemented with _erbout for example. I have just faced this issue last week, then my colleague suggested me like that "Don't you render it in a view?" (in Japanese). Yes, in a view biding, every helpers must be work correctly.

The solution is like following code:

test/units/foo_helper_test.rb:
...

class FooTestController < ActionController::Base
...
def index
render :inline => <<-INLINE
<% foo do %>
<%= ... %>
<% end %>
INLINE
end

end

class FooHelperTest < Test::Unit::TestCase
def setup
@controller = FooTestController.new
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new
end

def test_foo
get :index
# do assert_select or whatever you like, to check rendering result of the helper.
end
end

1 comment:

hiroshi saito said...

If you have problem about "nil.rewrite" Fix nil.rewrite errors in your Helper Tests may save you. Thanks for the information, Junya.