how to test view helpers in rails

Thursday, August 9th, 2007

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

# 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
      tag_options = tag_options(html_options)
      tag_options = nil
    url = url_for(:controller => controller_name) +
    "<a href=\"#{url}\"#{tag_options}>#{name || url}</a>"


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 = do
      attr_accessor :url, :request
      def url_for(options, *parameters_for_method_reference)
    @controller =
    # setting the url of the controller
    @controller.url = ""

  # testing the view method
  def test_time_link_to
    assert_equal "<a href=\"" +
      "#{}/#{}/" +
      time_link_to("testcontroller", "test",
    assert_equal "<a href=\"" +
      "#{}/#{}/" +
      "#{}\" id=\"test_id\">test</a>",
      time_link_to("testcontroller", "test",, {:id => "test_id"})

how to improve your presentation skills

Thursday, April 5th, 2007

on projectmanagement source there is a nice post on how to become a better public speaker. they list up 27 hints in form of best practices which can help to improve one’s presentation performance. i think there are two more important aspects:

28. try to get feedback: once you’ve finished your speech try to get feedback from your audience in form of a standardised questionnaire that you can hand out before. you can ask about

  • the form or quality of your presentation,
  • the innovativeness regarding the topic,
  • the auditors’ gain of experience,
  • the auditors’ readiness to recommend your speech to friends,
  • the auditors’ overall opinion,
  • and suggestions for improvement.

this gives you some important information about the things you still have to work on, and an impression on how successful your presentation really was.

29. be prepared for equipment failure: let’s assume you want to use some state-of-the-art presentation equipment like a mike, a notebook, a beamer, powerpoint and a wireless presenter mouse. the first thing is that you should allow for some extra time before you start your speech in order to get all this stuff working. if you encounter problems, you must be ready for an alternative solution (i.e. slices for an overhead projector).

there’s also a website which deals with “the art of speaking” and has some more nice hints.