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 online protection

Rails Long Term Support

Rails LTS provides security patches for old versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2)

  • Prevents you from data breaches and liability risks
  • Upgrade at your own pace
  • Works with modern Rubies
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)