class RSpecJsonMatchers::ApiResponseMatcher

General matcher for API responses, used to compose matchers for the overall API response or individual model serializations.

Attributes

results_with_errors[R]

Public Class Methods

new(expected, object_definition: nil, object_name: nil, context: nil) click to toggle source
# File lib/rspec_json_matchers/api_response_matcher.rb, line 7
def initialize(expected,
               object_definition: nil,
               object_name: nil,
               context: nil)
  @expected = expected
  @object_definition = object_definition
  @object_name = object_name
  @context = context
end

Public Instance Methods

description() click to toggle source

If we have explicitly defined the type of API matcher we want, report that, otherwise describe the general API matcher @return [String]

# File lib/rspec_json_matchers/api_response_matcher.rb, line 58
def description
  description = surface_descriptions_in(@expected).inspect

  if @object_name
    "a serialized #{@object_name}" \
      "#{" matching #{description}" if description != '{}'}"
  else
    "an api response matching #{description}"
  end
end
failure_message() click to toggle source

Overrides `failure_message` to return our own field specific failure messsages with color! @return [String]

# File lib/rspec_json_matchers/api_response_matcher.rb, line 37
def failure_message
  "Expected API response to match specification:\n#{pretty_results}"
end
failure_message_when_negated() click to toggle source

Overrides `failure_message_when_negated` when testing that an API response does not include a resource. @return [String]

# File lib/rspec_json_matchers/api_response_matcher.rb, line 44
def failure_message_when_negated
  "Expected API response not to match specification, but it did:\n#{pretty_results}"
end
inspect() click to toggle source

Overridden because we don't want to inspect `@object_definition` and `@context` @return [String]

# File lib/rspec_json_matchers/api_response_matcher.rb, line 72
def inspect
  "#<ApiResponseMatcher#{" for #{@object_name}" if @object_name}>"
end
matches?(actual) click to toggle source

Use our implementation of FuzzyMatcher to test the given data against our expected data. @param expected [Object] Expected values or matchers @param actual [Object] Actual data to be validated @return [Boolean]

# File lib/rspec_json_matchers/api_response_matcher.rb, line 22
def matches?(actual)
  @actual = actual

  if @object_definition
    combined = @expected.reverse_merge @object_definition.to_hash(@context)
  end

  @did_match, @results_with_errors =
    FuzzyMatcher.match_values(combined || @expected, @actual)
  @did_match
end
pretty_results() click to toggle source

Formats our results object with PP and, resetting the color at the beginning of each line to override RSpec's default coloring. @return [String]

# File lib/rspec_json_matchers/api_response_matcher.rb, line 51
def pretty_results
  @results_with_errors.pretty_inspect.gsub(/^/, "\e[0m")
end