Skip to content

Commit

Permalink
Converts to string for comparing w/ hash_including
Browse files Browse the repository at this point in the history
  This change updates so the expected request and actual request,
  are compared to as strings, when using hash_including
  • Loading branch information
davidbegin committed Sep 17, 2015
1 parent 0678821 commit 3caeea8
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 3 deletions.
7 changes: 6 additions & 1 deletion lib/webmock/matchers/hash_including_matcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ def initialize(expected)
end

def ==(actual)
@expected.all? {|k,v| actual.has_key?(k) && v === actual[k]}
@expected.all? do |k,v|
actual_value = actual.respond_to?(:has_key?) ? actual[k] : actual
actual_value = WebMock::Util::QueryValueStringifier.stringify(actual_value)
actual.has_key?(k) && WebMock::Util::QueryValueStringifier.stringify(v) === actual_value
end

rescue NoMethodError
false
end
Expand Down
8 changes: 6 additions & 2 deletions lib/webmock/util/query_value_stringifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@ class QueryValueStringifier
class << self
def stringify(value)
case value
when String
when String, NilClass
value
when Array
value.map { |v| stringify(v) }
when Hash
Hash[value.map { |k, v| [k, stringify(v)] }]
else
when Integer, TrueClass, FalseClass, Symbol
value.to_s
when WebMock::Matchers::AnyArgMatcher, RSpec::Mocks::ArgumentMatchers::AnyArgMatcher
value
else
value
end
end
end
Expand Down
27 changes: 27 additions & 0 deletions spec/hash_including_bug_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require "spec_helper"
begin
require "httparty"
rescue LoadError
@skip_specs = true
end

unless @skip_specs
RSpec.describe "Hash including Bug" do
let!(:dummy_url) { 'http://dummyurl.com' }

it "receive a request" do
stub_request(:get, dummy_url).
with(query: hash_including({ param1: 5 })).
to_return(body: 'body 1')

expect(
HTTParty.get(dummy_url, {
query: {
param1: 5,
param2: 'random1'
}
}).body
).to eq 'body 1'
end
end
end
1 change: 1 addition & 0 deletions webmock.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Gem::Specification.new do |s|
s.add_development_dependency 'excon', '>= 0.27.5'
s.add_development_dependency 'minitest', '~> 5.0.0'
s.add_development_dependency 'rdoc', ((RUBY_VERSION == '1.8.6') ? '<= 3.5.0' : '>3.5.0')
s.add_development_dependency 'httparty' if RUBY_VERSION >= "1.9.3"

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

0 comments on commit 3caeea8

Please sign in to comment.