开发者

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

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, J开发者_如何学JAVASON 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?


Instead of calling a private method, you can send a message:

myobject.send(:method_name, args)

This should avoid any problems accessing the private method. Perhaps more elegantly, if you're using rSpec you can do this:

before(:each) do
  MyClass.send(:public, *MyClass.protected_instance_methods)  
end

and then in your tests you can call the method normally.


Check out either Netrecorder(http://github.com/chrisyoung/netrecorder) or Fakeweb(http://github.com/chrisk/fakeweb).

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜