Class: JsonResponseMatchers::Values

Inherits:
Base
  • Object
show all
Defined in:
lib/json_response_matchers/values.rb

Instance Attribute Summary

Attributes inherited from Base

#actual, #expected

Instance Method Summary collapse

Methods inherited from Base

#at_key, #diffable?

Constructor Details

#initialize(expected) ⇒ Values

Returns a new instance of Values.



6
7
8
9
# File 'lib/json_response_matchers/values.rb', line 6

def initialize expected
  super
  @check_order = false
end

Instance Method Details

#failure_messageObject



11
12
13
# 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) ⇒ Object



25
26
27
28
# File 'lib/json_response_matchers/values.rb', line 25

def for *attributes
  @for = attributes.map &:to_s
  self
end

#in_strict_orderObject



30
31
32
33
# File 'lib/json_response_matchers/values.rb', line 30

def in_strict_order
  @check_order = true
  self
end

#matches?(actual) ⇒ Boolean

Returns:

  • (Boolean)

Raises:

  • (ArgumentError)


16
17
18
19
20
21
22
23
# 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