Skip to content

Commit

Permalink
Allow Savon mocks to receive any message. Closes savonrb#402
Browse files Browse the repository at this point in the history
Previously, there was no way to create a mock without expectation of the
specific message to be sent to the SOAP action.

Contrary to the documentation, leaving out the `with` method would result in an
expectation that the action be called without a message.

This change adds a special case to `#with` so that calling `#with(message: :any)`
ends up setting an expectation that the SOAP action be called, but
without caring about the actual message sent to the action.

This way the following will pass:

    savon.expects(:authenticate).with(message: :any)
    client.call(:authenticate)
    client.call(:authenticate, message: { username: "luke", password: "secret" })
  • Loading branch information
koppen committed May 12, 2013
1 parent bd476de commit 907999c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
2 changes: 1 addition & 1 deletion docs/version2.md
Original file line number Diff line number Diff line change
Expand Up @@ -1080,7 +1080,7 @@ but can easily be changed to support any global and or local option along with t
This is possible because Savon mocks the request as late as possible to ensure everything works as expected
in your integration tests.

If you're trying to "stub" a request, you can simply leave out the `#with` method, but you need to call the
If you're trying to "stub" a request, you can pass `message: any` to the `#with` method to accept any message. You still need to call the
`#returns` method to return a response that Savon can work with.

``` ruby
Expand Down
1 change: 1 addition & 0 deletions lib/savon/mock/expectation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ def verify_operation_name!
end

def verify_message!
return if @expected[:message] == :any
unless @expected[:message] == @actual[:message]
expected_message = " with this message: #{@expected[:message].inspect}" if @expected[:message]
expected_message ||= " with no message."
Expand Down
14 changes: 14 additions & 0 deletions spec/savon/mock_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,20 @@
" with this message: #{message.inspect}")
end

it "does not fail when any message is expected and an actual message" do
savon.expects(:find_user).with(:message => :any).returns("<fixture/>")
message = { :username => "luke" }

expect { new_client.call(:find_user, :message => message) }.to_not raise_error
end

it "does not fail when any message is expected and no actual message" do
savon.expects(:find_user).with(:message => :any).returns("<fixture/>")

expect { new_client.call(:find_user) }.to_not raise_error
end


it "allows code to rescue Savon::Error and still report test failures" do
message = { :username => "luke" }
savon.expects(:find_user).with(:message => message).returns("<fixture/>")
Expand Down

0 comments on commit 907999c

Please sign in to comment.