Ravn::SpecHelpers::WebHelpers::

HaveJSONCollectionMatcher class

RSpec matcher for matching Rack::Test response body from a collection endpoint

Expect that the response is a JSON Array of Objects:

expect( last_response ).to have_json_collection

Expect that there be 4 Objects in the collection:

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

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

expect( last_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( last_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( last_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( last_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( last_response ).to have_json_collection. with_same_ids_as( payments ).in_same_order

Add aggregate matchers for each object in the collection:

expect( last_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 )

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

# File lib/ravn/spec_helpers/web_helpers.rb, line 412
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()

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/ravn/spec_helpers/web_helpers.rb, line 406
def describe_type_expectation
        return "a JSON collection (Array of Objects)"
end
in_same_order()

Enforce ordering when matching IDs.

# File lib/ravn/spec_helpers/web_helpers.rb, line 445
def in_same_order
        @order_enforced = true
        return self
end
matches?( response )

Overridden to include matching against collection IDs.

# File lib/ravn/spec_helpers/web_helpers.rb, line 390
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 )

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

# File lib/ravn/spec_helpers/web_helpers.rb, line 453
def with_fields( *fieldset )
        return self.and_all( include *fieldset )
end
Also aliased as: and_fields
with_ids( *expected_ids )

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

# File lib/ravn/spec_helpers/web_helpers.rb, line 421
def with_ids( *expected_ids )
        self.and_all( include :id )
        @expected_ids = expected_ids.flatten( 1 )
        return self
end
with_same_ids_as( *objects )

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/ravn/spec_helpers/web_helpers.rb, line 431
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?()

Returns true if the collection contains exactly the IDs specified by

with_same_ids_as, or if no IDs were specified.

# File lib/ravn/spec_helpers/web_helpers.rb, line 465
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()

Overridden to set the expected type to Array.

# File lib/ravn/spec_helpers/web_helpers.rb, line 364
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