Class: BeJsonApiResponseFor
- Inherits:
-
Object
- Object
- BeJsonApiResponseFor
- Includes:
- JsonapiRspec
- Defined in:
- lib/jsonapi_rspec/be_json_api_response_for.rb
Overview
Class BeJsonApiResponseFor provides custom RSpec matching for json:api responses for a given object instance. It checks attributes and elements iteratively and fails on the first mismatch that it finds.
It expects a Rack::Response (or similar) object to be passed as the left-side of the comparison and a regular Object-derived instance as the right-side.
Usage:
expect(response).to BeJsonApiResponseFor.new(object_instance)
Constant Summary
Constants included from JsonapiRspec
Instance Method Summary collapse
-
#initialize(object_instance, plural_form = nil) ⇒ BeJsonApiResponseFor
constructor
A new instance of BeJsonApiResponseFor.
- #matches?(response) ⇒ Boolean
Methods included from JsonapiRspec
configure, #failure_message, #failure_message_when_negated, root
Constructor Details
#initialize(object_instance, plural_form = nil) ⇒ BeJsonApiResponseFor
Returns a new instance of BeJsonApiResponseFor.
21 22 23 24 |
# File 'lib/jsonapi_rspec/be_json_api_response_for.rb', line 21 def initialize(object_instance, plural_form = nil) @object_instance = object_instance @plural_form = plural_form end |
Instance Method Details
#matches?(response) ⇒ Boolean
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/jsonapi_rspec/be_json_api_response_for.rb', line 26 def matches?(response) return false unless valid_response?(response) @parsed_response = JSON.parse(response.body) return false if response_is_error? return false unless valid_data_section? if JsonapiRspec.configuration. return false unless end @parsed_response.each do |key, value| case key.to_sym when :data return false unless match_object?(value) when :meta return false unless when :jsonapi next # this can legally be anything when :included next # TODO: handle included objects when :links next # TODO: handle links objects else return (FailureMessages::UNEXPECTED_TOP_LVL_KEY % key) end end true end |