Class: RSpec::GraphqlAssistant::Matchers::HaveAttributes

Inherits:
Object
  • Object
show all
Includes:
MatcherHelper
Defined in:
lib/rspec/graphql_assistant/matchers/have_attributes.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from MatcherHelper

#key_to_array, #lookup_value

Constructor Details

#initialize(key, expected, **args) ⇒ HaveAttributes

Returns a new instance of HaveAttributes.



9
10
11
12
13
# File 'lib/rspec/graphql_assistant/matchers/have_attributes.rb', line 9

def initialize(key, expected, **args)
  @key = key
  @expected = expected
  @root = args.fetch(:root, :data)
end

Instance Attribute Details

#expectedObject (readonly)

Returns the value of attribute expected.



7
8
9
# File 'lib/rspec/graphql_assistant/matchers/have_attributes.rb', line 7

def expected
  @expected
end

#keyObject (readonly)

Returns the value of attribute key.



7
8
9
# File 'lib/rspec/graphql_assistant/matchers/have_attributes.rb', line 7

def key
  @key
end

#rootObject (readonly)

Returns the value of attribute root.



7
8
9
# File 'lib/rspec/graphql_assistant/matchers/have_attributes.rb', line 7

def root
  @root
end

#sampleObject (readonly)

Returns the value of attribute sample.



7
8
9
# File 'lib/rspec/graphql_assistant/matchers/have_attributes.rb', line 7

def sample
  @sample
end

Instance Method Details

#descriptionObject



35
36
37
# File 'lib/rspec/graphql_assistant/matchers/have_attributes.rb', line 35

def description
  "'#{key}' has attributes '#{expected}'"
end

#expected_hash(value, expected) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/rspec/graphql_assistant/matchers/have_attributes.rb', line 22

def expected_hash(value, expected)
  res = true
  expected.each do |k, v|
    res = value[k] == v
    break unless res
  end
  res
end

#failure_messageObject



31
32
33
# File 'lib/rspec/graphql_assistant/matchers/have_attributes.rb', line 31

def failure_message
  "'#{key}' not equal attributes '#{expected}'"
end

#matches?(actual) ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
18
19
20
# File 'lib/rspec/graphql_assistant/matchers/have_attributes.rb', line 15

def matches?(actual)
  value = lookup_value(actual)
  return expected_hash(value, expected) if expected.is_a?(Hash)

  false
end