Ravn::SpecHelpers::WebHelpers::
HaveJSONBodyMatcher class
Superclass | Object |
Included Modules |
|
Extended With |
|
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) ) )
Attributes
- additional_expectations R
- expected_type R
- failure_description R
- response R
Public Class Methods
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.
Public Instance Methods
Add the specified matchers
as expectations of the Hash or Array that’s parsed from the JSON body.
RSpec matcher API – return a message describing an expectation failure.
RSpec matcher API – return a message describing an expectation being met when the matcher was used in a negated context.
RSpec matcher API – returns true
if all expectations of the specified response
are met.
Add an additional expectation that the JSON body contain the specified number
of members.
Return the response’s body parsed as JSON.
Add an additional expectation that the JSON body does not contain the specified members
.
Add an additional expectation that the JSON body contains the specified members
.
Protected Instance Methods
Returns true
if the response has a JSON content-type header.
Check that the JSON body of the response has the correct type, if a type was specified.
Return an Array of descriptions of the members that were expected to be included in the response body, if any were specified. If none were specified, returns an empty Array.
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.
Return false
after setting the failure message to message
.
Check that any additional matchers registered via the .and
mutator also match the parsed response body.
Return a String that contains a pretty-printed version of the response object.
Return a String that contains a pretty-printed version of the response body.
Return a String that contains a pretty-printed version of the response headers.