Class: RequestInterceptor::Matchers::InterceptedRequest
- Inherits:
-
Object
- Object
- RequestInterceptor::Matchers::InterceptedRequest
- Defined in:
- lib/request_interceptor/matchers.rb
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#method ⇒ Object
readonly
Returns the value of attribute method.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#query ⇒ Object
readonly
Returns the value of attribute query.
-
#transactions ⇒ Object
readonly
Returns the value of attribute transactions.
Instance Method Summary collapse
-
#count(count = nil) ⇒ Object
Chains.
- #description ⇒ Object
- #failure_message ⇒ Object
- #failure_message_when_negated ⇒ Object
-
#initialize(method, path) ⇒ InterceptedRequest
constructor
A new instance of InterceptedRequest.
-
#matches?(transactions) ⇒ Boolean
Rspec Matcher Protocol.
- #with_body(body) ⇒ Object
- #with_query(query) ⇒ Object
Constructor Details
#initialize(method, path) ⇒ InterceptedRequest
Returns a new instance of InterceptedRequest.
19 20 21 22 23 24 |
# File 'lib/request_interceptor/matchers.rb', line 19 def initialize(method, path) @method = method @path = path @count = (1..Float::INFINITY) @transactions = [] end |
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the value of attribute body.
16 17 18 |
# File 'lib/request_interceptor/matchers.rb', line 16 def body @body end |
#method ⇒ Object (readonly)
Returns the value of attribute method.
13 14 15 |
# File 'lib/request_interceptor/matchers.rb', line 13 def method @method end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
14 15 16 |
# File 'lib/request_interceptor/matchers.rb', line 14 def path @path end |
#query ⇒ Object (readonly)
Returns the value of attribute query.
15 16 17 |
# File 'lib/request_interceptor/matchers.rb', line 15 def query @query end |
#transactions ⇒ Object (readonly)
Returns the value of attribute transactions.
17 18 19 |
# File 'lib/request_interceptor/matchers.rb', line 17 def transactions @transactions end |
Instance Method Details
#count(count = nil) ⇒ Object
Chains
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/request_interceptor/matchers.rb', line 30 def count(count = nil) return @count if count.nil? @count = case count when Integer (count .. count) when Range count else raise ArgumentError end self end |
#description ⇒ Object
115 116 117 |
# File 'lib/request_interceptor/matchers.rb', line 115 def description "should intercept a #{format_method(method)} request to #{path}" end |
#failure_message ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/request_interceptor/matchers.rb', line 81 def expected_request = "#{format_method(method)} #{path}" expected_request += " with query #{format_object(query)}" if query expected_request += " and" if query && body expected_request += " with body #{format_object(body)}" if body similar_intercepted_requests = similar_transactions.map.with_index do |transaction, index| method = format_method(transaction.request.method) path = transaction.request.path query = transaction.request.query body = transaction.request.body indentation_required = index != 0 = "#{method} #{path}" += (query.nil? || query.empty?) ? " with no query" : " with query #{format_object(query)}" += (body.nil? || body.empty?) ? " and with no body" : " and with body #{format_object(body)}" = " " * 10 + if indentation_required end similar_intercepted_requests = ["none"] if similar_intercepted_requests.none? "\nexpected: #{expected_request}" \ "\n got: #{similar_intercepted_requests.join("\n")}" end |
#failure_message_when_negated ⇒ Object
107 108 109 110 111 112 113 |
# File 'lib/request_interceptor/matchers.rb', line 107 def = "intercepted a #{format_method(method)} request to #{path}" += " with query #{format_object(query)}" if query += " and" if query && body += " with body #{format_object(body)}" if body end |
#matches?(transactions) ⇒ Boolean
Rspec Matcher Protocol
76 77 78 79 |
# File 'lib/request_interceptor/matchers.rb', line 76 def matches?(transactions) @transactions = transactions count.cover?(matching_transactions.count) end |
#with_body(body) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/request_interceptor/matchers.rb', line 59 def with_body(body) body_matcher = if body.respond_to?(:matches?) && body.respond_to?(:failure_message) body else RSpec::Matchers::BuiltIn::Eq.new(body) end @body = MatcherWrapper.new(body_matcher) self end |
#with_query(query) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/request_interceptor/matchers.rb', line 46 def with_query(query) query_matcher = if query.respond_to?(:matches?) && query.respond_to?(:failure_message) query else RSpec::Matchers::BuiltIn::Eq.new(query) end @query = MatcherWrapper.new(query_matcher) self end |