开发者

RSpec approach to test XML and HTTP responses?

开发者 https://www.devze.com 2023-04-04 08:16 出处:网络
I have a RESTful site that uses both the XML and web responses (API and web site). Since there are a lot of pages, my current goal is setting up RSpec to simply request each of the pages in both data

I have a RESTful site that uses both the XML and web responses (API and web site). Since there are a lot of pages, my current goal is setting up RSpec to simply request each of the pages in both data formats and check if the returned response is 200. What is the best way to check for both XML and HTTP 200 response? I know I should be doing TDD upfront, but right now I need this as a shell.

Example: I want to request both "/users" and "/u开发者_如何学Pythonsers.xml" and test if there weren't any server errors (200 OK)


I wrote a blog post on testing JSON APIs with RSpec a couple of weeks ago.

Basically, the way we are doing it is to get the actual response, and parse it to make sure it has the right content. As an example:

context "#index (GET /artworks.json)" do
  # create 30 Artwork documents using FactoryGirl, and do a HTTP GET request on "/artworks.json"
  before(:each) do
    30.times { FactoryGirl.create(:artwork) }
    get "/artworks.json"
  end
  describe "should list all artworks" do
    # the request returns a variable called "response", which we can then make sure comes back as expected
    it { response.should be_ok }
    it { JSON.parse(response.body)["results"].should be_a_kind_of(Array) }
    it { JSON.parse(response.body)["results"].length.should eq 30 }
    # etc...
  end
end

Obviously a simple example, but hopefully you get the idea. I hope this helps.

0

精彩评论

暂无评论...
验证码 换一张
取 消