在 Controller 规范中,我可以像这样设置 http 接受 header :
request.accept = "application/json"
但在请求规范中,“请求”对象为零。那么我在这里怎么做呢?
我想将http接受 header 设置为json的原因是我可以这样做:
get '/my/path'
而不是这个
get '/my/path.json'
最佳答案
您应该能够将 HTTP header 指定为 get() 方法的第三个参数,如下所述:
http://api.rubyonrails.org/classes/ActionDispatch/Integration/RequestHelpers.html#method-i-get
这里
http://api.rubyonrails.org/classes/ActionDispatch/Integration/Session.html#method-i-process
所以,你可以试试这样的:
get '/my/path', nil, {'HTTP_ACCEPT' => "application/json"}
关于ruby-on-rails - 如何在 rspec 请求规范中设置请求 header ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9654465/