Skip to content

Commit

Permalink
Allow :any IN expected hash as value
Browse files Browse the repository at this point in the history
This to accomodate for example random (or hard to mock) values in the message.

In our case this was needed when the request/message needed a random RequestID.

In theory we would be able to mock this random value, on the other hand we needed to introduce a mocking library in our test sutie just for this.
  • Loading branch information
nathansamson authored and tjarratt committed May 26, 2015
1 parent 8fa970e commit 385f785
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions lib/savon/mock/expectation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,26 @@ def verify_operation_name!

def verify_message!
return if @expected[:message].eql? :any
unless @expected[:message] === @actual[:message]
unless equals_except_any(@expected[:message], @actual[:message])
expected_message = " with this message: #{@expected[:message].inspect}" if @expected[:message]
expected_message ||= " with no message."

actual_message = " with this message: #{@actual[:message].inspect}" if @actual[:message]
actual_message ||= " with no message."

raise ExpectationError, "Expected a request to the #{@expected[:operation_name].inspect} operation\n#{expected_message}\n" \
"Received a request to the #{@actual[:operation_name].inspect} operation\n#{actual_message}"
"Received a request to the #{@actual[:operation_name].inspect} operation\n#{actual_message}"
end
end

def equals_except_any(msg_expected, msg_real)
return true if msg_expected === msg_real
return false if (msg_expected.nil? || msg_real.nil?) # If both are nil has returned true
msg_expected.each do |key, expected_value|
next if (expected_value == :any && msg_real.include?(key))
return false if expected_value != msg_real[key]
end
return true
end
end
end

0 comments on commit 385f785

Please sign in to comment.