当我加载脚本/控制台时,有时我想使用控制器或视图助手方法的输出。
有没有办法:
模拟一个请求? 从控制器实例调用方法说请求? 测试助手方法,无论是通过所说的控制器实例或其他方式?
当我加载脚本/控制台时,有时我想使用控制器或视图助手方法的输出。
有没有办法:
模拟一个请求? 从控制器实例调用方法说请求? 测试助手方法,无论是通过所说的控制器实例或其他方式?
当前回答
下面是如何以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
其他回答
另一种方法是使用Ruby on Rails调试器。http://guides.rubyonrails.org/debugging_rails_applications.html上有一个关于调试的Ruby on Rails指南
基本上,使用-u选项启动服务器:
./script/server -u
然后在脚本中插入一个断点,在这个断点中您可以访问控制器、助手等。
class EventsController < ApplicationController
def index
debugger
end
end
当您发出请求并点击代码中的该部分时,服务器控制台将返回一个提示符,然后您可以从命令提示符发出请求、查看对象等。完成后,只需输入'cont'继续执行。也有扩展调试的选项,但这至少可以让您开始。
在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控制台中测试Helper方法的一种可能方法是:
Struct.new(:t).extend(YourHelper).your_method(*arg)
和重载做:
reload!; Struct.new(:t).extend(YourHelper).your_method(*arg)
下面是如何以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
你可以像下面这样在Ruby on Rails控制台中访问你的方法:
controller.method_name
helper.method_name