如何在 rspec 请求规范中设置请求标头? [英] How to set request headers in rspec request spec?

查看:31
本文介绍了如何在 rspec 请求规范中设置请求标头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在控制器规范中,我可以像这样设置 http 接受标头:

In the controller spec, I can set http accept header like this:

request.accept = "application/json"

但在请求规范中,请求"对象为零.那么我怎么能在这里做呢?

but in the request spec, "request" object is nil. So how can I do it here?

我想将 http 接受头设置为 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/类/ActionDispatch/Integration/Session.html#method-i-process

所以,你可以尝试这样的事情:

So, you can try something like this:

get '/my/path', nil, {'HTTP_ACCEPT' => "application/json"}

这篇关于如何在 rspec 请求规范中设置请求标头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆