class JsonResponseMatchers::Values

Public Class Methods

new(expected) click to toggle source
Calls superclass method JsonResponseMatchers::Base::new
# File lib/json_response_matchers/values.rb, line 6
def initialize expected
  super
  @check_order = false
end

Public Instance Methods

failure_message() click to toggle source
# File lib/json_response_matchers/values.rb, line 11
def failure_message
  "expected\n    #{@actual}\nto #{@check_order ? 'equal' : 'match'}\n    #{@expected}"
end
for(*attributes) click to toggle source
# File lib/json_response_matchers/values.rb, line 25
def for *attributes
  @for = attributes.map &:to_s
  self
end
in_strict_order() click to toggle source
# File lib/json_response_matchers/values.rb, line 30
def in_strict_order
  @check_order = true
  self
end
matches?(actual) click to toggle source
# File lib/json_response_matchers/values.rb, line 16
def matches? actual
  raise ArgumentError, "You must set one or more attributes for mapping with #for:\n    have_json_values(#{@expected}).for(attribute_name(s))" unless @for
  @actual = fetch_from extract_parsed_json_from actual
  @actual = @actual.map do |item|
    @for.one? ? item[@for.first] : @for.map { |attribute| item[attribute] }
  end
  @check_order ? @actual == @expected : arrays_match?
end

Private Instance Methods

arrays_match?() click to toggle source
# File lib/json_response_matchers/values.rb, line 38
def arrays_match?
  expected = @expected.dup
  all_matches = @actual.all? do |actual_value|
    if expected.include? actual_value
      expected.delete actual_value
      true
    end
  end
  all_matches && expected.empty?
end