当我加载脚本/控制台时,有时我想使用控制器或视图助手方法的输出。

有没有办法:

模拟一个请求? 从控制器实例调用方法说请求? 测试助手方法,无论是通过所说的控制器实例或其他方式?


当前回答

在Ruby on Rails控制台中测试Helper方法的一种可能方法是:

Struct.new(:t).extend(YourHelper).your_method(*arg)

和重载做:

reload!; Struct.new(:t).extend(YourHelper).your_method(*arg)

其他回答

如果你已经添加了自己的helper,并且希望它的方法在控制台中可用,请执行以下操作:

在控制台执行包含YourHelperName 你的助手方法现在在控制台中可用,在控制台中使用它们调用method_name(args)。

例如:假设你在'app/helpers/my_helper中有MyHelper(使用my_method方法)。Rb ',然后在控制台执行:

包括MyHelper my_helper.my_method

下面是如何以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控制台中实例化控制器对象。

例如,

class CustomPagesController < ApplicationController

  def index
    @customs = CustomPage.all
  end

  def get_number
    puts "Got the Number"
  end

  protected

  def get_private_number
    puts 'Got private Number'
  end

end

custom = CustomPagesController.new
2.1.5 :011 > custom = CustomPagesController.new
 => #<CustomPagesController:0xb594f77c @_action_has_layout=true, @_routes=nil, @_headers={"Content-Type"=>"text/html"}, @_status=200, @_request=nil, @_response=nil>
2.1.5 :014 > custom.get_number
Got the Number
 => nil

# For calling private or protected methods,
2.1.5 :048 > custom.send(:get_private_number)
Got private Number
 => nil

这里有一种方法可以通过控制台做到这一点:

>> 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控制台中测试Helper方法的一种可能方法是:

Struct.new(:t).extend(YourHelper).your_method(*arg)

和重载做:

reload!; Struct.new(:t).extend(YourHelper).your_method(*arg)