views:

620

answers:

2

I'm using rails 2.2.2 and wondering how can I set the params values to test my helper methods.

I found some examples to let you run tests with helper methods but it doesn't work for me when I use the request or params value directly in the method.

require 'test_helper'

class ProductHelperTest < Test::Unit::TestCase
  include ProductHelper

  context 'ProductHelper' do
    should 'build the link' do
      assert_equal '', build_link
    end
  end
end

When using the request or params value I'll get an error that the local variable or method is undefined. How would I go about setting the value?

Error from shoulda when using the request value and it will be the same messages when using the params value.

1) Error:
test: ProductHelper should build the link. (ProductHelperTest):
NameError: undefined local variable or method `request` for #<ProductHelperTest:0x33ace6c>
  /vendor/rails/actionpack/lib/action_controller/test_process.rb:471:in `method_missing`
  /app/helpers/products_helper.rb:14:in `build_link`
  ./test/unit/product_helper_test.rb:10:in `__bind_1251902384_440357`
  /vendor/gems/thoughtbot-shoulda-2.0.5/lib/shoulda/context.rb:254:in `call`
  /vendor/gems/thoughtbot-shoulda-2.0.5/lib/shoulda/context.rb:254:in `test: ProductHelper should build the link. `
  /vendor/rails/activesupport/lib/active_support/testing/setup_and_teardown.rb:94:in `__send__`
  /vendor/rails/activesupport/lib/active_support/testing/setup_and_teardown.rb:94:in `run`
+1  A: 

I guess you have to mock out calls to request and params using mocha or by defining mock objects in your test:

# Assuming ProductHelper implementation
module ProductHelper
  def build_link
    "#{request.path}?#{params[:id]}"
  end
end

class ProductHelperTest < Test::Unit::TestCase
  include ProductHelper

  # mock by defining a method
  def params
    { :controller => "test", :id => "23" }
  end

  # mock request.path using `mocha` # => "my_url"
  def request
    mock(:path => "my_url")
  end

  context 'ProductHelper' do
    should 'build the link' do
      assert_equal 'my_url?23', build_link
    end
  end
end

I hope this helps :)

splattael
Thanks, this solution works perfectly. One change I made to it for the params and request functions were to return @request and @params so that I can redefine what should be returned in the context setups because the functions can only be redefined once per class.
Vizjerai
Vizjerai - Do you have an example of this? I tried... @params = {}; def params; @params; end;But keep receiving nil.[] errors.
Douglas F Shearer
A: 

As a note, if you are using Rails 2.3.x or anything which uses ActionView::TestCase - then all you really need to do is just have a private params method defined in your test.

e.g

require 'test_helper'

class BookmarksHelperTest < ActionView::TestCase

  context "with an applied filter of my bookmarks" do
    setup do
      expects(:applied_filters).returns({:my_bookmarks => true})
    end

    should "not be able to see it when other filters are called using my_bookmarks_filter" do
      assert_equal other_filters(:my_bookmarks), {}
    end
  end

  private

    def params
      {}
    end

end

You could even do one better by defining params as a method inside of ActionView::TestCase

Omar Qureshi