当我加载脚本/控制台
时,有时我想使用 Controller 或 View 帮助器方法的输出。
有没有办法:
- 模拟请求?
- 根据所述请求从 Controller 实例调用方法?
- 通过所述 Controller 实例或其他方式测试辅助方法?
最佳答案
要调用帮助程序,请使用 helper
对象:
$ ./script/console
>> helper.number_to_currency('123.45')
=> "R$ 123,45"
如果您想使用默认情况下未包含的帮助程序(例如,因为您从 ApplicationController
中删除了 helper :all
),则只需包含该帮助程序即可。
>> include BogusHelper
>> helper.bogus
=> "bogus output"
至于处理 Controller ,我引用Nick's答案:
> app.get '/posts/1' > response = app.response # you now have a rails response object much like the integration tests > response.body # get you the HTML > response.cookies # hash of the cookies # etc, etc
关于ruby-on-rails - 如何从 Ruby on Rails 的控制台调用 Controller / View 辅助方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/151030/