Wednesday, March 23, 2011

How do I test helpers in Rails?

I'm trying to build some unit tests for testing my Rails helpers, but I can never remember how to access them. Annoying. Suggestions?

From stackoverflow
  • Stolen from here: http://joakimandersson.se/archives/2006/10/05/test-your-rails-helpers/

    require File.dirname(__FILE__) + ‘/../test_helper’
    require ‘user_helper’
    
    class UserHelperTest < Test::Unit::TestCase
    
    include UserHelper
    
    def test_a_user_helper_method_here
    end
    
    end
    

    [Stolen from Matt Darby, who also wrote in this thread.] You can do the same in RSpec as:

    require File.dirname(__FILE__) + '/../spec_helper'
    
    describe FoosHelper do
    
      it "should do something" do
        helper.some_helper_method.should == @something
      end
    
    end
    
  • You can do the same in RSpec as:

    require File.dirname(__FILE__) + '/../spec_helper'
    
    describe FoosHelper do
    
      it "should do something" do
        helper.some_helper_method.should == @something
      end
    
    end
    
    aronchick : it's times like this i wish i could approve two answers. Would you mind copying and pasting in my answer below into your answer and I'll make it the answer to this question?

0 comments:

Post a Comment