Skip to content

Commit 20fa242

Browse files
committed
Fix rspec matcher to check nested hashes individually
1 parent 8556377 commit 20fa242

File tree

1 file changed

+29
-22
lines changed

1 file changed

+29
-22
lines changed

api-spec-testing/rspec_matchers.rb

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -175,32 +175,39 @@ def sanitize_pairs(expected_pairs)
175175

176176
def mismatched_values(pairs, test, response)
177177
@mismatched_values ||= begin
178-
179178
if pairs.is_a?(String)
180179
# Must return an empty list if there are no mismatched values
181180
compare_string_response(pairs, response) ? [] : [ pairs ]
182181
else
183-
pairs.select do |expected_key, expected_value|
184-
# Select the values that don't match, used for the failure message.
185-
186-
split_key = TestFile::Test.split_and_parse_key(expected_key).collect do |k|
187-
test.get_cached_value(k)
188-
end
189-
actual_value = TestFile::Test.find_value_in_document(split_key, response)
190-
191-
# Sometimes the expected value is a cached value from a previous request.
192-
# See test api_key/10_basic.yml
193-
expected_value = test.get_cached_value(expected_value)
194-
195-
# When you must match a regex. For example:
196-
# match: {task: '/.+:\d+/'}
197-
if expected_value.is_a?(String) && expected_value[0] == "/" && expected_value[-1] == "/"
198-
!( /#{expected_value.tr("/", "")}/ =~ actual_value)
199-
elsif expected_key == ''
200-
expected_value != response
201-
else
202-
actual_value != expected_value
203-
end
182+
compare_hash(pairs, response, test)
183+
end
184+
end
185+
end
186+
187+
def compare_hash(expected_keys_values, response, test)
188+
expected_keys_values.reject do |expected_key, expected_value|
189+
# Select the values that don't match, used for the failure message.
190+
191+
if expected_value.is_a?(Hash)
192+
compare_hash(response[expected_key], expected_value, test)
193+
elsif expected_value.is_a?(String)
194+
split_key = TestFile::Test.split_and_parse_key(expected_key).collect do |k|
195+
test.get_cached_value(k)
196+
end
197+
actual_value = TestFile::Test.find_value_in_document(split_key, response)
198+
199+
# Sometimes the expected value is a cached value from a previous request.
200+
# See test api_key/10_basic.yml
201+
expected_value = test.get_cached_value(expected_value)
202+
203+
# When you must match a regex. For example:
204+
# match: {task: '/.+:\d+/'}
205+
if expected_value.is_a?(String) && expected_value[0] == "/" && expected_value[-1] == "/"
206+
/#{expected_value.tr("/", "")}/ =~ actual_value
207+
elsif expected_key == ''
208+
expected_value == response
209+
else
210+
actual_value == expected_value
204211
end
205212
end
206213
end

0 commit comments

Comments
 (0)