在 rspec 中指定内容类型 [英] Specifying Content Type in rspec
本文介绍了在 rspec 中指定内容类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试构建一个通过 POST 发送 JSON(或 XML)的 rspec 测试.但是,我似乎无法真正让它工作:
I'm trying to build an rspec test that sends JSON (or XML) via POST. However, I can't seem to actually get it working:
json = {.... data ....}.to_json
post '/model1.json',json,{'CONTENT_TYPE'=>'application/json'}
还有这个
json = {.... data ....}.to_json
post '/model1.json',json,{'Content-Type'=>'application/json'}
有什么想法吗?谢谢!
推荐答案
在 Rails 3 中,你可以跳过 header 和 @request.env
的东西,只需添加一个 format
> 后调用的参数,例如:
In Rails 3, you can skip the header and @request.env
stuff and just add a format
parameter to your post call, e.g.:
post :create, format: :json, param: 'Value of Param'
这篇关于在 rspec 中指定内容类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文