Class: Webhookdb::SpecHelpers::Service::HaveJSONBodyMatcher

Inherits:
Object
  • Object
show all
Includes:
Appydays::Loggable, RSpec::Matchers
Defined in:
lib/webhookdb/spec_helpers/service.rb

Overview

RSpec matcher for matching Rack::Test response body

Expect that the response consists of JSON of some sort:

expect( last_response ).to have_json_body

Expect that it’s a JSON body that deserializes as an Object:

expect( last_response ).to have_json_body( Object )
# -or-
expect( last_response ).to have_json_body( Hash )

Expect that it’s a JSON body that deserializes as an Array:

expect( last_response ).to have_json_body( Array )

Expect that it’s a JSON body that deserializes as an Object that has expected keys:

expect( last_response ).to have_json_body( Object ).
    that_includes( :id, :first_name, :last_name )

Expect that it’s a JSON body that deserializes as an Object that has expected keys and values:

expect( last_response ).to have_json_body( Object ).
    that_includes(
        id: 118,
        first_name: 'Princess',
        last_name: 'Buttercup'
    )

Expect that it’s a JSON body that has other expected stuff:

expect( last_response ).to have_json_body( Object ).
    that_includes(
        last_name: a_string_matching(/humperdink/i),
        profile: a_hash_including(:age, :eyecolor, :tracking_ability)
    )

Expect a JSON Array with objects that all match the criteria:

expect( last_response ).to have_json_body( Array ).
    of_lenth( 20 ).
    and( all( be_an(Integer) ) )

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(expected_type = nil) ⇒ HaveJSONBodyMatcher

Create a new matcher that expects a response with a JSON body. If expected_type is not specified, any JSON body will be sufficient for a match.



147
148
149
150
151
152
# File 'lib/webhookdb/spec_helpers/service.rb', line 147

def initialize(expected_type=nil)
  @expected_type = expected_type
  @additional_expectations = []
  @response = nil
  @failure_description = nil
end

Instance Attribute Details

#additional_expectationsObject (readonly)

Returns the value of attribute additional_expectations.



154
155
156
# File 'lib/webhookdb/spec_helpers/service.rb', line 154

def additional_expectations
  @additional_expectations
end

#expected_typeObject (readonly)

Returns the value of attribute expected_type.



154
155
156
# File 'lib/webhookdb/spec_helpers/service.rb', line 154

def expected_type
  @expected_type
end

#failure_descriptionObject (readonly)

Returns the value of attribute failure_description.



154
155
156
# File 'lib/webhookdb/spec_helpers/service.rb', line 154

def failure_description
  @failure_description
end

#responseObject (readonly)

Returns the value of attribute response.



154
155
156
# File 'lib/webhookdb/spec_helpers/service.rb', line 154

def response
  @response
end

Instance Method Details

#and(*matchers) ⇒ Object

Add the specified matchers as expectations of the Hash or Array that’s parsed from the JSON body.



225
226
227
228
# File 'lib/webhookdb/spec_helpers/service.rb', line 225

def and(*matchers)
  @additional_expectations.concat(matchers)
  return self
end

#failure_messageObject

RSpec matcher API – return a message describing an expectation failure.



171
172
173
174
175
176
# File 'lib/webhookdb/spec_helpers/service.rb', line 171

def failure_message
  return "\n---\n%s\n---\n\nReason: %s\n" % [
    self.pretty_print_response,
    self.failure_description,
  ]
end

#failure_message_when_negatedObject

RSpec matcher API – return a message describing an expectation being met when the matcher was used in a negated context.



180
181
182
183
184
185
186
187
188
189
190
# File 'lib/webhookdb/spec_helpers/service.rb', line 180

def failure_message_when_negated
  msg = "expected response not to have a %s" % [self.describe_type_expectation]
  msg << " and " << self.describe_additional_expectations.join(", ") unless
    self.additional_expectations.emtpy?
  msg << ", but it did."

  return "\n---\n%s\n---\n\nReason: %s\n" % [
    self.pretty_print_response,
    msg,
  ]
end

#matches?(response) ⇒ Boolean

RSpec matcher API – returns true if all expectations of the specified response are met.

Returns:

  • (Boolean)


161
162
163
164
165
166
167
168
# File 'lib/webhookdb/spec_helpers/service.rb', line 161

def matches?(response)
  @response = response
  return self.correct_content_type? &&
      self.correct_json_type? &&
      self.matches_additional_expectations?
rescue StandardError => e
  return self.fail_with "Response has invalid JSON body: %s: %s" % [e.class.name, e.message]
end

#of_length(number) ⇒ Object Also known as: of_size

Add an additional expectation that the JSON body contain the specified number of members.



217
218
219
220
# File 'lib/webhookdb/spec_helpers/service.rb', line 217

def of_length(number)
  @additional_expectations << have_attributes(length: number)
  return self
end

#parsed_response_bodyObject

Return the response’s body parsed as JSON.



193
194
195
# File 'lib/webhookdb/spec_helpers/service.rb', line 193

def parsed_response_body
  return @parsed_response_body ||= JSON.parse(self.response.body, symbolize_names: true)
end

#that_excludes(*memberset) ⇒ Object

Add an additional expectation that the JSON body does not contain the specified members.



210
211
212
213
# File 'lib/webhookdb/spec_helpers/service.rb', line 210

def that_excludes(*memberset)
  @additional_expectations << exclude(*memberset)
  return self
end

#that_includes(*memberset) ⇒ Object Also known as: which_includes

Add an additional expectation that the JSON body contains the specified members.



202
203
204
205
# File 'lib/webhookdb/spec_helpers/service.rb', line 202

def that_includes(*memberset)
  @additional_expectations << include(*memberset)
  return self
end