小编gaa*_*ner的帖子

使用Rspec,如何在Rails 3.0.11中测试我的控制器的JSON格式?

我已经浏览了网页,但是,唉,我似乎无法让Rspec正确发送内容类型,所以我可以测试我的JSON API.我正在使用RABL gem用于模板,Rails 3.0.11和Ruby 1.9.2-p180.

我的卷曲输出,工作正常(应该是401,我知道):

mrsnuggles:tmp gaahrdner$ curl -i -H "Accept: application/json" -X POST -d @bleh http://localhost:3000/applications
HTTP/1.1 403 Forbidden 
Content-Type: application/json; charset=utf-8
Cache-Control: no-cache
X-Ua-Compatible: IE=Edge
X-Runtime: 0.561638
Server: WEBrick/1.3.1 (Ruby/1.9.2/2011-02-18)
Date: Tue, 06 Mar 2012 01:10:51 GMT
Content-Length: 74
Connection: Keep-Alive
Set-Cookie: _session_id=8e8b73b5a6e5c95447aab13dafd59993; path=/; HttpOnly

{"status":"error","message":"You are not authorized to access this page."}
Run Code Online (Sandbox Code Playgroud)

我的一个测试用例中的示例:

describe ApplicationsController do
  render_views
  disconnect_sunspot

  let(:application) { Factory.create(:application) }

  subject { application }

  context "JSON" do

    describe "creating a new application" do

      context …
Run Code Online (Sandbox Code Playgroud)

api json rspec ruby-on-rails header

58
推荐指数
2
解决办法
6万
查看次数

标签 统计

api ×1

header ×1

json ×1

rspec ×1

ruby-on-rails ×1