Class: SerialSpec::RequestResponse::ProvideMatcher::Provide

Inherits:
Object
  • Object
show all
Defined in:
lib/serial_spec/request_response/provide_matcher.rb

Defined Under Namespace

Classes: SerializerNotFound

Constant Summary collapse

HYPERMEDIA_ATTRIBUTES =
["links", "includes"]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(expected, options = {}) ⇒ Provide

Returns a new instance of Provide.



21
22
23
24
25
26
27
28
# File 'lib/serial_spec/request_response/provide_matcher.rb', line 21

def initialize(expected,options={})
  @expected       = expected
  @as_serializer  = options[:as]

  if @as_serializer and not @as_serializer.instance_methods.include?(:serializable_hash)
    raise ArgumentError, 'must be an active model serializer'
  end
end

Instance Attribute Details

#actualObject (readonly)

Returns the value of attribute actual.



17
18
19
# File 'lib/serial_spec/request_response/provide_matcher.rb', line 17

def actual
  @actual
end

#as_serializerObject (readonly)

Returns the value of attribute as_serializer.



15
16
17
# File 'lib/serial_spec/request_response/provide_matcher.rb', line 15

def as_serializer
  @as_serializer
end

#expectedObject (readonly)

Returns the value of attribute expected.



16
17
18
# File 'lib/serial_spec/request_response/provide_matcher.rb', line 16

def expected
  @expected
end

Instance Method Details

#actual_to_hash(actual) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/serial_spec/request_response/provide_matcher.rb', line 30

def actual_to_hash(actual)
  if actual.kind_of? SerialSpec::ParsedBody
    strip_hypermedia(actual.execute)
  else
    strip_hypermedia(actual)
  end
end

#collection_serializerObject



49
50
51
52
53
54
55
# File 'lib/serial_spec/request_response/provide_matcher.rb', line 49

def collection_serializer
  if as_serializer
    ActiveModel::ArraySerializer.new(expected, serializer: as_serializer, root: nil )
  else
    ActiveModel::ArraySerializer.new(expected, root: nil)
  end
end

#expected_to_hashObject

to_json first to normalize hash and all it’s members the parse into JSON to compare to ParsedBody hash



59
60
61
62
63
64
65
66
# File 'lib/serial_spec/request_response/provide_matcher.rb', line 59

def expected_to_hash
  if expected.kind_of?(Array)
    #hack
    JSON.parse(collection_serializer.as_json.to_json)
  else
    JSON.parse(resource_serializer.to_json)
  end
end

#failed_message(msg) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/serial_spec/request_response/provide_matcher.rb', line 105

def failed_message(msg)
  case msg
  when :response_and_model_dont_match
    "Actual and Expected do not match.\nActual   #{actual}\nExpected #{expected}"
  when :serializer_not_specified_on_class
    "'active_model_serializer' not implemented on expected, see ehttp://bit.ly/18TdmXs"
  when :response_not_valid
    "Actual not valid Hash or Array.\nActual: #{actual}"
  else
    "no failed_message found for #{msg}"
  end
end

#failure_messageObject



118
119
120
# File 'lib/serial_spec/request_response/provide_matcher.rb', line 118

def failure_message
  @failure_message || "error should TO  implement"
end

#failure_message_when_negatedObject



122
123
124
# File 'lib/serial_spec/request_response/provide_matcher.rb', line 122

def failure_message_when_negated
  @failure_message || "error should not  TO implement"
end

#matches?(actual) ⇒ Boolean Also known as: ==

Returns:

  • (Boolean)


82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/serial_spec/request_response/provide_matcher.rb', line 82

def matches?(actual)
  failure = catch(:failed) do

    unless actual.kind_of?(Hash) || actual.kind_of?(Array) || actual.kind_of?(ParsedBody)
      throw(:failed, :response_not_valid)
    end

    @actual    = actual_to_hash(actual)
    @expected  = expected_to_hash

    if @actual == @expected
      #noop - specs pass
    else
      throw(:failed, :response_and_model_dont_match)
    end
  end
  @failure_message = failed_message(failure) if failure
  !failure
end

#normalize_data(data) ⇒ Object



68
69
70
71
72
73
74
75
76
# File 'lib/serial_spec/request_response/provide_matcher.rb', line 68

def normalize_data(data)
  if data.kind_of?(Array)
    data.each_with_index do |el, index|
      data[index] = normalize_data(el)
    end
  elsif data.kind_of?(Hash)
    data.deep_stringify_keys!.to_hash
  end
end

#resource_serializerObject



38
39
40
41
42
43
44
45
46
47
# File 'lib/serial_spec/request_response/provide_matcher.rb', line 38

def resource_serializer
  if as_serializer
    as_serializer.new(expected, root: nil)
  else
    unless expected.respond_to?(:active_model_serializer)
      throw(:failed, :serializer_not_specified_on_class)
    end
    expected.active_model_serializer.new(expected, root: nil)
  end
end

#strip_hypermedia(actual) ⇒ Object



78
79
80
# File 'lib/serial_spec/request_response/provide_matcher.rb', line 78

def strip_hypermedia(actual)
  (actual || {}).delete_if {|k,v| HYPERMEDIA_ATTRIBUTES.include?(k) }
end