1

如何在我的 controller_spec 代码中包含一些帮助程序?我在 dates_ads_helper 中有一个名为“a_title(ads)”的方法,并返回 self。如何在我的 controller_spec 测试中使用此方法?

当我尝试在我的 controller_spec 文件中调用它时,我收到了这个错误

NoMethodError:
       undefined method `a_title' 
4

1 回答 1

1

要使用模板引擎中已经包含的辅助方法:

Rails 2:使用@template 变量。Rails 3:有很好的控制器方法 view_context 使用 a_title

# rails 3 sample
def controller_action
  @price = view_context.a_title( 42.0 ) 
end

# rails 2 sample
def controller_action
  @price = @template.a_title( 42.0 ) 
end

如果你想在助手和控制器之间共享一个方法,你可以通过助手方法定义:

class SomeController < ActionController::Base
  helper_method :my_shared_method
  ...

  def my_shared_method
    #do stuff
  end
end

问候!

于 2012-11-08T13:48:05.273 回答