3

How to avoid this error:

Error: test_update_location(LocationControllerTest)
NoMethodError: undefined method `show_previous_version' for test_update_location(LocationControllerTest):LocationControllerTest/usr/local/rvm/gems/ruby-1.9.3-p327/gems/actionpack-2.1.1/lib/action_controller/test_process.rb:467:in `method_missing'

I want to test the helper method show_previous_version defined in app/helpers/description_helper.rb:

def show_previous_version(obj)
    ...
  return html
end

In app/helpers/application _helper.rb:

module ApplicationHelper
  .....
  require_dependency 'description_helper'
  ...
end

In test/functional/location_controller_test.rb

def test_update_location
  ...
  loc = Location.find(loc.id)
  html = show_previous_version(loc)
  ...
end

When I run the tests, I get:

Error: test_update_location(LocationControllerTest)
NoMethodError: undefined method `show_previous_version' for test_update_location(LocationControllerTest):LocationControllerTest/usr/local/rvm/gems/ruby-1.9.3-p327/gems/actionpack-2.1.1/lib/action_controller/test_process.rb:467:in `method_missing'
user2069311
  • 134
  • 1
  • 12

1 Answers1

0

Helper methods are available to the controller instance, not on the test itself. Either include the helper directly in the test (messy), or use the controller (or some other object that includes the helper) to call that method.

To test using the controller, you can use the @controller instance variable inside an ActionController::TestCase:

class LocationControllerTest < ActionController::TestCase

  def test_update_location
    ...
    loc = Location.find(loc.id)
    html = @controller.show_previous_version(loc)
    ...
  end
end
PinnyM
  • 35,165
  • 3
  • 73
  • 81
  • Much thank for the information about helper methods being unavailable directly in the test. To follow up on your suggestion, can you give a simple example of using a controller -- in a test -- to call a controller helper method? – user2069311 Mar 19 '13 at 00:41
  • Helper methods are NOT available to the controller instance by default. – Maxim Kholyavkin Jan 16 '16 at 20:00
  • @Speakus ApplicationHelper is included by default in Rails 3, so I'm not sure what point you are making. – PinnyM Jan 17 '16 at 02:20
  • @PinnyM here about controller and helper: http://stackoverflow.com/a/2388943/751932 – Maxim Kholyavkin Jan 17 '16 at 03:14