How to test the XML sent to a web service in Ruby/Rails

Posted by Jason Langenauer on Stack Overflow See other posts from Stack Overflow or by Jason Langenauer
Published on 2010-06-03T02:05:02Z Indexed on 2010/06/03 2:14 UTC
Read the original article Hit count: 269

Filed under:
|
|

I'm looking for the best way to write unit test for code that POSTs to an external web service. The body of the POST request is an XML document which describes the actions and data for the web service to perform.

Now, I've wrapped the webservice in its own class (similar to ActiveResource), and I can't see any way to test the exact XML being generated by the class without breaking encapsulation by exposing some of the internal XML generation as public methods on the class. This seems to be a code smell - from the point-of-view of the users of the class, they should not know, nor care, how the class actually implements the web service call, be it with XML, JSON or carrier pigeons.

For an example of the class:

class Resource
  def new
    #initialize the class
  end

  def save!
    Http.post("http://webservice.com", self.to_xml)
  end

  private
  def to_xml
    # returns an XML representation of self
  end
end

I want to be able to test the XML generated to ensure it conforms to what the specs for the web service are expecting. So can I best do this, without making to_xml a public method?

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about ruby