Class: Strelka::Testing::HaveJSONCollectionMatcher

Inherits:
HaveJSONBodyMatcher show all
Includes:
RSpec::Matchers
Defined in:
lib/strelka/testing.rb

Overview

RSpec matcher for matching Strelka::HTTPResponse body from a collection endpoint

Expect that the response is a JSON Array of Objects:

expect( response ).to have_json_collection

Expect that there be 4 Objects in the collection:

expect( response ).to have_json_collection.of_length( 4 )

Expect that the collection’s objects each have an id field with the specified IDs:

expect( response ).to have_json_collection.with_ids( 3, 6, 11, 14 )
# -or- with an Array of IDs (no need to splat them)
ids = [3, 6, 11, 14]
expect( response ).to have_json_collection.with_ids( ids )

Expect that the collection’s objects have the same IDs as an Array of model objects (or other objects that respond to #pk):

payments = payment_fixture_factory.take( 4 )
expect( response ).to have_json_collection.
    with_same_ids_as( payments )

Expect that the collection’s objects have the same IDs as an Array of Hashes with :id fields:

payment_rows = payments_table.where( sender_id: 71524 ).all
expect( response ).to have_json_collection.
    with_same_ids_as( payment_rows )

Expect that the collection’s objects appear in the same order as the source Array:

payments = payment_fixture_factory.take( 4 )
expect( response ).to have_json_collection.
    with_same_ids_as( payments ).in_same_order

Add aggregate matchers for each object in the collection:

expect( response ).to have_json_collection.
    with_same_ids_as( payments ).
    and_all( include(amount_cents: a_value > 0) )

Instance Attribute Summary collapse

Attributes inherited from HaveJSONBodyMatcher

#additional_expectations, #expected_type, #failure_description, #response

Instance Method Summary collapse

Methods inherited from HaveJSONBodyMatcher

#and, #failure_message, #failure_message_when_negated, #of_length, #parsed_response_body, #that_excludes, #that_includes

Constructor Details

#initializeHaveJSONCollectionMatcher

Overridden to set the expected type to Array.



478
479
480
481
482
483
484
485
486
487
488
# File 'lib/strelka/testing.rb', line 478

def initialize # :notnew:
	super( Array )

	@additional_expectations << all( be_a Hash )

	@expected_ids   = nil
	@collection_ids = nil
	@extra_ids      = nil
	@missing_ids    = nil
	@order_enforced = false
end

Instance Attribute Details

#collection_idsObject (readonly)

Sets of IDs, actual vs. expected



496
497
498
# File 'lib/strelka/testing.rb', line 496

def collection_ids
  @collection_ids
end

#expected_idsObject (readonly)

Sets of IDs, actual vs. expected



496
497
498
# File 'lib/strelka/testing.rb', line 496

def expected_ids
  @expected_ids
end

#extra_idsObject (readonly)

Sets of IDs, actual vs. expected



496
497
498
# File 'lib/strelka/testing.rb', line 496

def extra_ids
  @extra_ids
end

#missing_idsObject (readonly)

Sets of IDs, actual vs. expected



496
497
498
# File 'lib/strelka/testing.rb', line 496

def missing_ids
  @missing_ids
end

#order_enforcedObject (readonly)

Sets of IDs, actual vs. expected



496
497
498
# File 'lib/strelka/testing.rb', line 496

def order_enforced
  @order_enforced
end

Instance Method Details

#and_all(*matchers) ⇒ Object

Add the specified matchers as expectations of each member of the collection.



526
527
528
529
530
# File 'lib/strelka/testing.rb', line 526

def and_all( *matchers )
	matchers = matchers.map {|m| all( m ) }
	@additional_expectations.concat( matchers )
	return self
end

#describe_type_expectationObject

Return an Array of text describing the expectation that the body be an Object or an Array, if a type was expected. If no type was expected, returns an empty Array.



520
521
522
# File 'lib/strelka/testing.rb', line 520

def describe_type_expectation
	return "a JSON collection (Array of Objects)"
end

#in_same_orderObject

Enforce ordering when matching IDs.



559
560
561
562
# File 'lib/strelka/testing.rb', line 559

def in_same_order
	@order_enforced = true
	return self
end

#matches?(response) ⇒ Boolean

Overridden to include matching against collection IDs.

Returns:

  • (Boolean)


504
505
506
507
508
509
510
511
512
513
514
# File 'lib/strelka/testing.rb', line 504

def matches?( response )
	return false unless super( response )

	if @expected_ids
		@collection_ids = self.parsed_response_body.collect {|obj| obj[:id] }
		@extra_ids = @collection_ids - @expected_ids
		@missing_ids = @expected_ids - @collection_ids
	end

	return self.has_required_ids?
end

#with_fields(*fieldset) ⇒ Object Also known as: and_fields

Adds an expectation that all members of the resulting collection have each of the keys in the specified fieldset.



567
568
569
# File 'lib/strelka/testing.rb', line 567

def with_fields( *fieldset )
	return self.and_all( include *fieldset )
end

#with_ids(*expected_ids) ⇒ Object

Set the expectation that the given expected_ids will be present as the values of the :id field of the collection.



535
536
537
538
539
# File 'lib/strelka/testing.rb', line 535

def with_ids( *expected_ids )
	self.and_all( include :id )
	@expected_ids = expected_ids.flatten( 1 )
	return self
end

#with_same_ids_as(*objects) ⇒ Object

Add an expectation that the collection’s objects all have an ‘:id’ field, and that the corresponding values be the same as the primary key values of the given objects (fetched via their #pk methods).



545
546
547
548
549
550
551
552
553
554
555
# File 'lib/strelka/testing.rb', line 545

def with_same_ids_as( *objects )
	objects.flatten!( 1 )

	ids = if objects.first.respond_to?( :pk )
			objects.flatten.map( &:pk )
		else
			objects.map {|obj| obj[:id] }
		end

	return self.with_ids( *ids )
end