Read more

Savon testing: How to expect any message

Arne Hartherz
May 07, 2015Software engineer at makandra GmbH

When using Savon Show archive.org snapshot to connect a SOAP API, you may want to use Savon::SpecHelper to mock requests in your tests as described in their documentation Show archive.org snapshot .

Illustration web development

Do you need DevOps-experts?

Your development team has a full backlog? No time for infrastructure architecture? Our DevOps team is ready to support you!

  • We build reliable cloud solutions with Infrastructure as code
  • We are experts in security, Linux and databases
  • We support your dev team to perform
Read more Show archive.org snapshot

When sending a message body, the savon mock object requires a message to be set, like this:

savon.expects(:action_name).with(message: { user_id: 123 }).returns('<some xml>')

If you want to stub only the returned XML and do not care about request arguments, you can not omit with as Savon's helper will complain:

savon.expects(:action_name).returns('<some xml>')

^
Savon::ExpectationError:
Expected a request to the :action_name operation
with no message.
Received a request to the :action_name operation
with this message: {:user_id=>123}

While it is in the documentation, it's not very prominent: Savon accepts :any as a value for :message.

savon.expects(:action_name).with(message: :any).returns('<some xml>')

It then accepts any SOAP message and just returns your XML.


As a side note: You could use VCR to record your SOAP talk. In my case my tests had to talk to the same service the staging servers talk to, so any recorded results would contain some random other data. I decided to make some requests and store clean XML fixtures.

Posted by Arne Hartherz to makandra dev (2015-05-07 20:32)