如何在rspec请求规范中设置请求标头? [英] How to set request headers in rspec request spec?
问题描述
在控制器规格中,我可以这样设置http accept报头:
In the controller spec, I can set http accept header like this:
request.accept = "application/json"
,但是在请求规范中,"request"对象为nil.那我怎么在这里呢?
but in the request spec, "request" object is nil. So how can I do it here?
我想将http accept标头设置为json的原因是我可以这样做:
The reason I want to set http accept header to json is so I can do this:
get '/my/path'
代替此
get '/my/path.json'
推荐答案
您应该能够将HTTP标头指定为get()方法的第三个参数,如下所述:
You should be able to specify HTTP headers as the third argument to your get() method as described here:
http://api.rubyonrails.org/classes/ActionDispatch/Integration/RequestHelpers.html#method-i-get
在这里
http://api.rubyonrails.org/classes/ActionDispatch/Integration/Session.html#method-i-process
因此,您可以尝试执行以下操作:
So, you can try something like this:
get '/my/path', nil, {'HTTP_ACCEPT' => "application/json"}
这篇关于如何在rspec请求规范中设置请求标头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!