how to test view helpers in rails

as you probaby know, ruby on rails has a very handy test integration. you can easily write unit tests for your rich domain model and some functional tests (that can be roughly compared to http-units in the java world, but with integrated webserver) for the controller and views.

unfortunately, one part of rails basic implementation architecture cannot be tested as easy as the above mentioned components: the view helpers. actually they are used to extract view functionality and keep the view templates clean from ruby scriptlets. of course, you can test them indirectly through your functional tests, but rails provides no pure way to test them individually. an obvious approach is to write some unit-tests for each helper class. this works fine as long as you are not using some methods provided by rails, i.e. link_to, url_for, all the input tag methods and so on.

so here is an example of a simple view helper. the method time_link_to returns a link including the current date as parameter in the form http://www.yourhost.com/railscontroller/year/month/day.

# Methods added to this helper will be
# available to all templates in the application.
module ApplicationHelper

  def time_link_to(controller_name, name, date, html_options = nil)
    if html_options
      html_options = html_options.stringify_keys
      convert_options_to_javascript!(html_options)
      tag_options = tag_options(html_options)
    else
      tag_options = nil
    end
    url = url_for(:controller => controller_name) +
           "/#{date.year}/#{date.month}/#{date.day}"
    "<a href=\"#{url}\"#{tag_options}>#{name || url}</a>"
  end

end

since the method url_for is provided by the controller that is executed when the view and therefore the helper is evaluated, you have to fake it for the unit test. this can be done in the setup method of the unit test like this:

class ApplicationHelperTest < Test::Unit::TestCase

  include ActionView::Helpers::AssetTagHelper
  include ActionView::Helpers::UrlHelper
  include ActionView::Helpers::TagHelper
  include ApplicationHelper

  # called before every single test
  def setup
    # creating a dummy controller with the method url_for that
    # returns the url assigned to the controller
    @controller = Class.new do
      attr_accessor :url, :request
      def url_for(options, *parameters_for_method_reference)
        url
      end
    end
    @controller = @controller.new
    # setting the url of the controller
    @controller.url = "http://www.example.com"
  end

  # testing the view method
  def test_time_link_to
    assert_equal "<a href=\"http://www.example.com/testcontroller/" +
      "#{Date.today.year}/#{Date.today.month}/" +
      "#{Date.today.day}\">test</a>",
      time_link_to("testcontroller", "test", Date.today)
    assert_equal "<a href=\"http://www.example.com/testcontroller/" +
      "#{Date.today.year}/#{Date.today.month}/" +
      "#{Date.today.day}\" id=\"test_id\">test</a>",
      time_link_to("testcontroller", "test",
        Date.today, {:id => "test_id"})
  end
end

Related Posts

2 thoughts on “how to test view helpers in rails

Comments are closed.