2
如何避免这个错误:Rails的测试/单元测试的辅助方法,NoMethodError:未定义的方法
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'
我想测试中app/helpers/description_helper.rb
定义的helper方法show_previous_version
:
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'
感谢很多有关辅助方法在测试不可用直接的信息。为了跟进你的建议,你能给出一个简单的例子,使用控制器 - 在一个测试中 - 调用一个控制器辅助方法? – user2069311 2013-03-19 00:41:28
@ user2069311:为此更新。 – PinnyM 2013-03-20 14:02:05
默认情况下,控制器实例不能使用辅助方法。 – Speakus 2016-01-16 20:00:04