class Strelka::Testing::HaveJSONCollectionMatcher

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) )

Attributes

collection_ids[R]

Sets of IDs, actual vs. expected

expected_ids[R]

Sets of IDs, actual vs. expected

extra_ids[R]

Sets of IDs, actual vs. expected

missing_ids[R]

Sets of IDs, actual vs. expected

order_enforced[R]

Sets of IDs, actual vs. expected

Public Instance Methods

and_all( *matchers ) click to toggle source

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

# File lib/strelka/testing.rb, line 525
def and_all( *matchers )
        matchers = matchers.map {|m| all( m ) }
        @additional_expectations.concat( matchers )
        return self
end
and_fields( *fieldset )
Alias for: with_fields
describe_type_expectation() click to toggle source

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.

# File lib/strelka/testing.rb, line 519
def describe_type_expectation
        return "a JSON collection (Array of Objects)"
end
in_same_order() click to toggle source

Enforce ordering when matching IDs.

# File lib/strelka/testing.rb, line 558
def in_same_order
        @order_enforced = true
        return self
end
matches?( response ) click to toggle source

Overridden to include matching against collection IDs.

# File lib/strelka/testing.rb, line 503
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 ) click to toggle source

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

# File lib/strelka/testing.rb, line 566
def with_fields( *fieldset )
        return self.and_all( include *fieldset )
end
Also aliased as: and_fields
with_ids( *expected_ids ) click to toggle source

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

# File lib/strelka/testing.rb, line 534
def with_ids( *expected_ids )
        self.and_all( include :id )
        @expected_ids = expected_ids.flatten( 1 )
        return self
end
with_same_ids_as( *objects ) click to toggle source

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).

# File lib/strelka/testing.rb, line 544
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

Protected Instance Methods

has_required_ids?() click to toggle source

Returns true if the collection contains exactly the IDs specified by with_same_ids_as, or if no IDs were specified.

# File lib/strelka/testing.rb, line 578
def has_required_ids?
        return true unless @expected_ids

        if @order_enforced && @expected_ids != @collection_ids
                return self.fail_with "expected collection IDs to be %p, but they were: %p" %
                        [ @expected_ids, @collection_ids ]
        elsif @missing_ids && !@missing_ids.empty?
                return self.fail_with( "collection is missing expected IDs: %p" % [@missing_ids] )
        elsif @extra_ids && !@extra_ids.empty?
                return self.fail_with( "collection has extra IDs: %p" % [@extra_ids] )
        end

        return true
end
initialize() click to toggle source

Overridden to set the expected type to Array.

Calls superclass method
# File lib/strelka/testing.rb, line 477
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