当我加载脚本/控制台时,有时我想使用控制器或视图助手方法的输出。
有没有办法:
模拟一个请求? 从控制器实例调用方法说请求? 测试助手方法,无论是通过所说的控制器实例或其他方式?
当我加载脚本/控制台时,有时我想使用控制器或视图助手方法的输出。
有没有办法:
模拟一个请求? 从控制器实例调用方法说请求? 测试助手方法,无论是通过所说的控制器实例或其他方式?
当前回答
如果你需要从控制台进行测试(在Ruby on Rails 3.1和4.1上进行测试):
调用控制器操作:
app.get '/'
app.response
app.response.headers # => { "Content-Type"=>"text/html", ... }
app.response.body # => "<!DOCTYPE html>\n<html>\n\n<head>\n..."
控制器方法:
foo = ActionController::Base::ApplicationController.new
foo.public_methods(true||false).sort
foo.some_method
路线助手:
app.myresource_path # => "/myresource"
app.myresource_url # => "http://www.example.com/myresource"
视图助手:
foo = ActionView::Base.new
foo.javascript_include_tag 'myscript' #=> "<script src=\"/javascripts/myscript.js\"></script>"
helper.link_to "foo", "bar" #=> "<a href=\"bar\">foo</a>"
ActionController::Base.helpers.image_tag('logo.png') #=> "<img alt=\"Logo\" src=\"/images/logo.png\" />"
呈现:
views = Rails::Application::Configuration.new(Rails.root).paths["app/views"]
views_helper = ActionView::Base.new views
views_helper.render 'myview/mytemplate'
views_helper.render file: 'myview/_mypartial', locals: {my_var: "display:block;"}
views_helper.assets_prefix #=> '/assets'
ActiveSupport这样方法:
require 'active_support/all'
1.week.ago
=> 2013-08-31 10:07:26 -0300
a = {'a'=>123}
a.symbolize_keys
=> {:a=>123}
自由模块:
> require 'my_utils'
=> true
> include MyUtils
=> Object
> MyUtils.say "hi"
evaluate: hi
=> true
其他回答
你可以像下面这样在Ruby on Rails控制台中访问你的方法:
controller.method_name
helper.method_name
在Ruby on Rails 3中,试试这个:
session = ActionDispatch::Integration::Session.new(Rails.application)
session.get(url)
body = session.response.body
主体将包含URL的HTML。
如何路由和呈现(调度)从一个模型在Ruby on Rails 3
如果你需要从控制台进行测试(在Ruby on Rails 3.1和4.1上进行测试):
调用控制器操作:
app.get '/'
app.response
app.response.headers # => { "Content-Type"=>"text/html", ... }
app.response.body # => "<!DOCTYPE html>\n<html>\n\n<head>\n..."
控制器方法:
foo = ActionController::Base::ApplicationController.new
foo.public_methods(true||false).sort
foo.some_method
路线助手:
app.myresource_path # => "/myresource"
app.myresource_url # => "http://www.example.com/myresource"
视图助手:
foo = ActionView::Base.new
foo.javascript_include_tag 'myscript' #=> "<script src=\"/javascripts/myscript.js\"></script>"
helper.link_to "foo", "bar" #=> "<a href=\"bar\">foo</a>"
ActionController::Base.helpers.image_tag('logo.png') #=> "<img alt=\"Logo\" src=\"/images/logo.png\" />"
呈现:
views = Rails::Application::Configuration.new(Rails.root).paths["app/views"]
views_helper = ActionView::Base.new views
views_helper.render 'myview/mytemplate'
views_helper.render file: 'myview/_mypartial', locals: {my_var: "display:block;"}
views_helper.assets_prefix #=> '/assets'
ActiveSupport这样方法:
require 'active_support/all'
1.week.ago
=> 2013-08-31 10:07:26 -0300
a = {'a'=>123}
a.symbolize_keys
=> {:a=>123}
自由模块:
> require 'my_utils'
=> true
> include MyUtils
=> Object
> MyUtils.say "hi"
evaluate: hi
=> true
这里有一种方法可以通过控制台做到这一点:
>> foo = ActionView::Base.new
=> #<ActionView::Base:0x2aaab0ac2af8 @assigns_added=nil, @assigns={}, @helpers=#<ActionView::Base::ProxyModule:0x2aaab0ac2a58>, @controller=nil, @view_paths=[]>
>> foo.extend YourHelperModule
=> #<ActionView::Base:0x2aaab0ac2af8 @assigns_added=nil, @assigns={}, @helpers=#<ActionView::Base::ProxyModule:0x2aaab0ac2a58>, @controller=nil, @view_paths=[]>
>> foo.your_helper_method(args)
=> "<html>created by your helper</html>"
创建ActionView::Base的新实例可以让你访问你的助手可能使用的普通视图方法。然后扩展YourHelperModule,将它的方法混合到你的对象中,让你查看它们的返回值。
从脚本/控制台和视图/操作响应对象调用控制器动作的简单方法是:
> app.get '/posts/1'
> response = app.response
# You now have a Ruby on Rails response object much like the integration tests
> response.body # Get you the HTML
> response.cookies # Hash of the cookies
# etc., etc.
app对象是ActionController::Integration::Session的实例
在我使用Ruby on Rails 2.1和2.3时,这是可行的,我没有尝试更早的版本。