当我加载脚本/控制台时,有时我想使用控制器或视图助手方法的输出。
有没有办法:
模拟一个请求? 从控制器实例调用方法说请求? 测试助手方法,无论是通过所说的控制器实例或其他方式?
当我加载脚本/控制台时,有时我想使用控制器或视图助手方法的输出。
有没有办法:
模拟一个请求? 从控制器实例调用方法说请求? 测试助手方法,无论是通过所说的控制器实例或其他方式?
当前回答
这里有一种方法可以通过控制台做到这一点:
>> 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,将它的方法混合到你的对象中,让你查看它们的返回值。
其他回答
如果你需要从控制台进行测试(在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,将它的方法混合到你的对象中,让你查看它们的返回值。
要调用helper,请使用helper对象:
$ ./script/console
>> helper.number_to_currency('123.45')
=> "R$ 123,45"
如果你想使用一个默认情况下没有包含的helper(比如,因为你从ApplicationController中删除了helper:all),只需要包含这个helper。
>> include BogusHelper
>> helper.bogus
=> "bogus output"
至于如何处理控制器,我引用Nick的回答:
get '/posts/1' > response = app.response #您现在有了一个rails响应对象,就像集成测试一样 >反应。body #给你HTML >反应。cookie #散列的cookie # etc, etc
如果方法是POST方法,那么:
app.post 'controller/action?parameter1=value1¶meter2=value2'
(这里的参数将根据您的适用性而定。)
如果是GET方法,则:
app.get 'controller/action'
下面是如何以Refinery为例进行认证POST请求:
# Start Rails console
rails console
# Get the login form
app.get '/community_members/sign_in'
# View the session
app.session.to_hash
# Copy the CSRF token "_csrf_token" and place it in the login request.
# Log in from the console to create a session
app.post '/community_members/login', {"authenticity_token"=>"gT7G17RNFaWUDLC6PJGapwHk/OEyYfI1V8yrlg0lHpM=", "refinery_user[login]"=>'chloe', 'refinery_user[password]'=>'test'}
# View the session to verify CSRF token is the same
app.session.to_hash
# Copy the CSRF token "_csrf_token" and place it in the request. It's best to edit this in Notepad++
app.post '/refinery/blog/posts', {"authenticity_token"=>"gT7G17RNFaWUDLC6PJGapwHk/OEyYfI1V8yrlg0lHpM=", "switch_locale"=>"en", "post"=>{"title"=>"Test", "homepage"=>"0", "featured"=>"0", "magazine"=>"0", "refinery_category_ids"=>["1282"], "body"=>"Tests do a body good.", "custom_teaser"=>"", "draft"=>"0", "tag_list"=>"", "published_at(1i)"=>"2014", "published_at(2i)"=>"5", "published_at(3i)"=>"27", "published_at(4i)"=>"21", "published_at(5i)"=>"20", "custom_url"=>"", "source_url_title"=>"", "source_url"=>"", "user_id"=>"56", "browser_title"=>"", "meta_description"=>""}, "continue_editing"=>"false", "locale"=>:en}
如果你得到一个错误,你可能会发现这些也很有用:
app.cookies.to_hash
app.flash.to_hash
app.response # long, raw, HTML