Hello all, I''m trying to write a story for a rest resource. Here''s what I have so far: Story "Get prices for specific book", %{ As a client I want to get a list of prices for a specific book So that I can use it on my own application }, :type => RailsStory do Scenario "Requesting /books/:id/prices using GET" do Given "a regular rest request" When "visiting", "/books/:id/prices" do |path| get path end Then "it should return an xml file" end end How would I check that the format of my response is in xml? Also, is it possible to check the content of an xml file, to make sure that it''s not empty? Any other suggestions on what would make this story more efficient? Thanks again Olivier Dupuis -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20080620/fc8a7d96/attachment.html>
Attached is a custom matcher using hpricot, (and the spec for it) it has a "have_xpath matcher for checking xml. I has an earlier one using rexml but hpricot is faster, you can read about that here.... http://blog.wolfman.com/articles/2008/01/02/xpath-matchers-for-rspec This new hpricot one is moslty based on someone elses work, and I''ll credit them when I got my blog entry on it done. Olivier Dupuis wrote:> Hello all, > > I''m trying to write a story for a rest resource. > > Here''s what I have so far: > > Story "Get prices for specific book", %{ > As a client > I want to get a list of prices for a specific book > So that I can use it on my own application > }, :type => RailsStory do > > Scenario "Requesting /books/:id/prices using GET" do > Given "a regular rest request" > > When "visiting", "/books/:id/prices" do |path| > get path > end > > Then "it should return an xml file" > end > end > > How would I check that the format of my response is in xml? Also, is it > possible to check the content of an xml file, to make sure that it''s not > empty? Any other suggestions on what would make this story more efficient? > > Thanks again > > Olivier Dupuis > > > ------------------------------------------------------------------------ > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users-------------- next part -------------- A non-text attachment was scrubbed... Name: hpricot_matchers.rb Type: application/x-ruby Size: 2811 bytes Desc: not available URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20080620/efa50867/attachment-0002.bin> -------------- next part -------------- A non-text attachment was scrubbed... Name: test_hpricot_matchers.rb Type: application/x-ruby Size: 2147 bytes Desc: not available URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20080620/efa50867/attachment-0003.bin>
On Fri, Jun 20, 2008 at 9:32 AM, Olivier Dupuis <olivierdupuis at gmail.com> wrote:> Hello all, > > I''m trying to write a story for a rest resource. > > Here''s what I have so far: > > Story "Get prices for specific book", %{ > As a client > I want to get a list of prices for a specific book > So that I can use it on my own application > }, :type => RailsStory do > > Scenario "Requesting /books/:id/prices using GET" do > Given "a regular rest request" > > When "visiting", "/books/:id/prices" do |path| > get path > end > > Then "it should return an xml file" > end > end > > How would I check that the format of my response is in xml? Also, is it > possible to check the content of an xml file, to make sure that it''s not > empty? Any other suggestions on what would make this story more efficient?I know a lot of people that do hpricot or xpath-style stuff to test XML. I really like my ghetto simple Hash.from_xml approach. hash = Hash.from_xml response.body hash[''book''].should_not be_nil hash[''book''][''price''].should == 123.45 You could probably wrap it up a bit more, but I like how clear and straightforward it is. Pat