Class: RSpec::JsonApi::Matchers::MatchJsonSchema

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec/json_api/matchers/match_json_schema.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(expected) ⇒ MatchJsonSchema

Returns a new instance of MatchJsonSchema.



9
10
11
# File 'lib/rspec/json_api/matchers/match_json_schema.rb', line 9

def initialize(expected)
  @expected = expected
end

Instance Attribute Details

#expectedObject (readonly)

Returns the value of attribute expected.



7
8
9
# File 'lib/rspec/json_api/matchers/match_json_schema.rb', line 7

def expected
  @expected
end

Instance Method Details

#failure_messageObject



30
31
32
# File 'lib/rspec/json_api/matchers/match_json_schema.rb', line 30

def failure_message
  self
end

#failure_message_when_negatedObject



34
35
36
# File 'lib/rspec/json_api/matchers/match_json_schema.rb', line 34

def failure_message_when_negated
  self
end

#matches?(actual) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/rspec/json_api/matchers/match_json_schema.rb', line 13

def matches?(actual)
  # Parse JSON to ruby object
  actual = JSON.parse(actual, symbolize_names: true)

  # Compare types
  return false unless actual.instance_of?(expected.class)

  if expected.instance_of?(Array)
    RSpec::JsonApi::CompareArray.compare(actual, expected)
  else
    # Compare actual and expected schema
    return false unless actual.deep_keys.deep_sort == expected.deep_keys.deep_sort

    RSpec::JsonApi::CompareHash.compare(actual, expected)
  end
end