Permalink
Browse files

Simpler way of checking whether body or query pattern is an RSpec Has…

…hIncludingMatcher
  • Loading branch information...
bblimke committed Mar 17, 2012
1 parent 369554f commit 0fe3c0249bf3f17efe980a716d9cf31fef9d9124
Showing with 11 additions and 4 deletions.
  1. +11 −4 lib/webmock/request_pattern.rb
@@ -1,5 +1,11 @@
module WebMock
+ module RSpecMatcherDetector
+ def rSpecHashIncludingMatcher?(matcher)
+ matcher.class.name =~ /R?Spec::Mocks::ArgumentMatchers::HashIncludingMatcher/
+ end
+ end
+
class RequestPattern
attr_reader :method_pattern, :uri_pattern, :body_pattern, :headers_pattern
@@ -73,6 +79,8 @@ def to_s
class URIPattern
+ include RSpecMatcherDetector
+
def initialize(pattern)
@pattern = pattern.is_a?(Addressable::URI) ? pattern : WebMock::Util::URI.normalize_uri(pattern)
end
@@ -82,8 +90,7 @@ def add_query_params(query_params)
query_params
elsif query_params.is_a?(WebMock::Matchers::HashIncludingMatcher)
query_params
- elsif (defined?(RSpec::Mocks::ArgumentMatchers::HashIncludingMatcher) && query_params.is_a?(RSpec::Mocks::ArgumentMatchers::HashIncludingMatcher)) ||
- (defined?(Spec::Mocks::ArgumentMatchers::HashIncludingMatcher) && query_params.is_a?(Spec::Mocks::ArgumentMatchers::HashIncludingMatcher))
+ elsif rSpecHashIncludingMatcher?(query_params)
WebMock::Matchers::HashIncludingMatcher.from_rspec_matcher(query_params)
else
Addressable::URI.parse('?' + query_params).query_values
@@ -145,6 +152,7 @@ def to_s
class BodyPattern
+ include RSpecMatcherDetector
BODY_FORMATS = {
'text/xml' => :xml,
@@ -162,8 +170,7 @@ class BodyPattern
def initialize(pattern)
@pattern = if pattern.is_a?(Hash)
normalize_hash(pattern)
- elsif (defined?(RSpec::Mocks::ArgumentMatchers::HashIncludingMatcher) && pattern.is_a?(RSpec::Mocks::ArgumentMatchers::HashIncludingMatcher)) ||
- (defined?(Spec::Mocks::ArgumentMatchers::HashIncludingMatcher) && pattern.is_a?(Spec::Mocks::ArgumentMatchers::HashIncludingMatcher))
+ elsif rSpecHashIncludingMatcher?(pattern)
WebMock::Matchers::HashIncludingMatcher.from_rspec_matcher(pattern)
else
pattern

0 comments on commit 0fe3c02

Please sign in to comment.