Class: BeJsonApiResponseFor
- Inherits:
-
Object
- Object
- BeJsonApiResponseFor
- 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)
Instance Method Summary collapse
- #failure_message ⇒ Object
- #failure_message_when_negated ⇒ Object
-
#initialize(object_instance) ⇒ BeJsonApiResponseFor
constructor
A new instance of BeJsonApiResponseFor.
- #matches?(response) ⇒ Boolean
Constructor Details
#initialize(object_instance) ⇒ BeJsonApiResponseFor
Returns a new instance of BeJsonApiResponseFor.
17 18 19 |
# File 'lib/jsonapi_rspec/be_json_api_response_for.rb', line 17 def initialize(object_instance) @object_instance = object_instance end |
Instance Method Details
#failure_message ⇒ Object
50 51 52 53 |
# File 'lib/jsonapi_rspec/be_json_api_response_for.rb', line 50 def ||= "Expected object [#{@object_instance}] to match" "#{@failure_message} - parsed response: #{pretty_response}" end |
#failure_message_when_negated ⇒ Object
55 56 57 58 |
# File 'lib/jsonapi_rspec/be_json_api_response_for.rb', line 55 def = "handle method 'failure_message_when_negated' in custom_matchers.rb" "#{@failure_message}: #{pretty_response}" end |
#matches?(response) ⇒ Boolean
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/jsonapi_rspec/be_json_api_response_for.rb', line 21 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 else return ("Unexpected key in response: '#{key}'") end end true end |