Class: Strelka::Testing::HaveJSONBodyMatcher

Inherits:
Object
  • Object
show all
Extended by:
Loggability
Includes:
RSpec::Matchers
Defined in:
lib/strelka/testing.rb

Overview

RSpec matcher for matching Strelka::HTTPResponse body

Expect that the response consists of JSON of some sort:

expect( response ).to have_json_body

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

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

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

expect( response ).to have_json_body( Array )

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

expect( 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( 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( 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( response ).to have_json_body( Array ).
    of_lenth( 20 ).
    and( all( be_an(Integer) ) )

Direct Known Subclasses

HaveJSONCollectionMatcher

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.



249
250
251
252
253
254
# File 'lib/strelka/testing.rb', line 249

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.



257
258
259
# File 'lib/strelka/testing.rb', line 257

def additional_expectations
  @additional_expectations
end

#expected_typeObject (readonly)

Returns the value of attribute expected_type.



257
258
259
# File 'lib/strelka/testing.rb', line 257

def expected_type
  @expected_type
end

#failure_descriptionObject (readonly)

Returns the value of attribute failure_description.



257
258
259
# File 'lib/strelka/testing.rb', line 257

def failure_description
  @failure_description
end

#responseObject (readonly)

Returns the value of attribute response.



257
258
259
# File 'lib/strelka/testing.rb', line 257

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.



338
339
340
341
# File 'lib/strelka/testing.rb', line 338

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

#failure_messageObject

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



277
278
279
280
281
282
# File 'lib/strelka/testing.rb', line 277

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.



287
288
289
290
291
292
293
294
295
296
297
# File 'lib/strelka/testing.rb', line 287

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)


265
266
267
268
269
270
271
272
273
# File 'lib/strelka/testing.rb', line 265

def matches?( response )
	@response = response

	return self.correct_content_type? &&
		self.correct_json_type? &&
		self.matches_additional_expectations?
rescue Yajl::ParseError => err
	return self.fail_with "Response has invalid JSON body: %s" % [ err.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.



329
330
331
332
# File 'lib/strelka/testing.rb', line 329

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

#parsed_response_bodyObject

Return the response’s body parsed as JSON.



301
302
303
304
# File 'lib/strelka/testing.rb', line 301

def parsed_response_body
	return @parsed_response_body ||=
		Yajl::Parser.parse( self.response.body, check_utf8: true, symbolize_keys: true )
end

#that_excludes(*memberset) ⇒ Object

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



321
322
323
324
# File 'lib/strelka/testing.rb', line 321

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.



312
313
314
315
# File 'lib/strelka/testing.rb', line 312

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