3

如何避免此错误:

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'

我想测试show_previous_version定义的帮助方法app/helpers/description_helper.rb

def show_previous_version(obj)
    ...
  return html
end

app/helpers/application _helper.rb

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

test/functional/location_controller_test.rb

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

当我运行测试时,我得到:

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'
4

1 回答 1

0

辅助方法可用于控制器实例,而不是测试本身。要么直接在测试中包含帮助器(混乱),要么使用控制器(或包含帮助器的其他对象)来调用该方法。

要使用控制器进行测试,您可以在 @controller 内部使用实例变量ActionController::TestCase

class LocationControllerTest < ActionController::TestCase

  def test_update_location
    ...
    loc = Location.find(loc.id)
    html = @controller.show_previous_version(loc)
    ...
  end
end
于 2013-03-18T16:18:12.453 回答