Savon testing: How to expect any message

Posted . Visible to the public.

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 .

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.

Arne Hartherz
Last edit
Arne Hartherz
License
Source code in this card is licensed under the MIT License.
Posted by Arne Hartherz to makandra dev (2015-05-07 18:32)