Skip to content

Commit 08a3a4a

Browse files
committed
Converts to string for comparing w/ hash_including
This change updates so the expected request and actual request, are compared to as strings, when using hash_including
1 parent 0678821 commit 08a3a4a

4 files changed

Lines changed: 40 additions & 3 deletions

File tree

lib/webmock/matchers/hash_including_matcher.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@ def initialize(expected)
88
end
99

1010
def ==(actual)
11-
@expected.all? {|k,v| actual.has_key?(k) && v === actual[k]}
11+
@expected.all? do |k,v|
12+
actual_value = actual.respond_to?(:has_key?) ? actual[k] : actual
13+
actual_value = WebMock::Util::QueryValueStringifier.stringify(actual_value)
14+
actual.has_key?(k) && WebMock::Util::QueryValueStringifier.stringify(v) === actual_value
15+
end
16+
1217
rescue NoMethodError
1318
false
1419
end

lib/webmock/util/query_value_stringifier.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,18 @@ class QueryValueStringifier
33
class << self
44
def stringify(value)
55
case value
6-
when String
6+
when String, NilClass
77
value
88
when Array
99
value.map { |v| stringify(v) }
1010
when Hash
1111
Hash[value.map { |k, v| [k, stringify(v)] }]
12-
else
12+
when Integer, TrueClass, FalseClass, Symbol
1313
value.to_s
14+
when WebMock::Matchers::AnyArgMatcher, RSpec::Mocks::ArgumentMatchers::AnyArgMatcher
15+
value
16+
else
17+
value
1418
end
1519
end
1620
end

spec/hash_including_bug_spec.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
require "spec_helper"
2+
begin
3+
require "httparty"
4+
rescue LoadError
5+
@skip_specs = true
6+
end
7+
8+
unless @skip_specs
9+
RSpec.describe "Hash including Bug" do
10+
let!(:dummy_url) { 'http://dummyurl.com' }
11+
12+
it "receive a request" do
13+
stub_request(:get, dummy_url).
14+
with(:query => hash_including({ :param1 => 5 })).
15+
to_return(:body => 'body 1')
16+
17+
expect(
18+
HTTParty.get(dummy_url, {
19+
:query => {
20+
:param1 => 5,
21+
:param2 => 'random1'
22+
}
23+
}).body
24+
).to eq 'body 1'
25+
end
26+
end
27+
end

webmock.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ Gem::Specification.new do |s|
3333
s.add_development_dependency 'excon', '>= 0.27.5'
3434
s.add_development_dependency 'minitest', '~> 5.0.0'
3535
s.add_development_dependency 'rdoc', ((RUBY_VERSION == '1.8.6') ? '<= 3.5.0' : '>3.5.0')
36+
s.add_development_dependency 'httparty' if RUBY_VERSION >= "1.9.3"
3637

3738
s.files = `git ls-files`.split("\n")
3839
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")

0 commit comments

Comments
 (0)