Not logged in (Log in or Sign up)

unwwwritten

A good helper is hard to test

So, you want to test your helpers?

I had been using the "helper_test" plugin, as described on nubyonrails.com. However, when I ran into a problem with that recently I dug into action_view to see how things were tested deep in the heart of rails country, and guess what?

Rails has that too. Go figure.

First, add require 'action_view/test_case' to test/test_helper.rb.

Then, test away...

   1  require File.dirname(__FILE__) + '/../../test_helper'
   2  
   3  class ApplicationHelperTest < ActionView::TestCase
   4    tests ApplicationHelper
   5  
   6    def test_should_render_sidebar
   7      expected = read_fixture('sidebar')
   8      assert_equal expected, sidebar('test').split("\n")
   9    end
  10    
  11  protected
  12    def read_fixture(fixture)
  13      IO.readlines(File.join(RAILS_ROOT, 'test', 'fixtures', 'application_helper', fixture)).collect(&:chomp)
  14    end
  15  
  16    # to prevent undefined method when forms are involved
  17    def protect_against_forgery?
  18      false
  19    end
  20  end
  21  

blog comments powered by Disqus