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 に答える