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

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
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(build_context, expected, options = {}) ⇒ Provide

Returns a new instance of Provide.



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

def initialize(build_context, expected, options={})
  @build_context  = build_context
  @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.



23
24
25
# File 'lib/serial_spec/request_response/provide_matcher.rb', line 23

def actual
  @actual
end

#as_serializerObject (readonly)

Returns the value of attribute as_serializer.



21
22
23
# File 'lib/serial_spec/request_response/provide_matcher.rb', line 21

def as_serializer
  @as_serializer
end

#expectedObject (readonly)

Returns the value of attribute expected.



22
23
24
# File 'lib/serial_spec/request_response/provide_matcher.rb', line 22

def expected
  @expected
end

Instance Method Details

#actual_to_hash(actual) ⇒ Object



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

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

#collection_serializerObject



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

def collection_serializer
  if as_serializer
    build_serializer ActiveModel::ArraySerializer, expected, serializer: as_serializer, root: nil
  else
    build_serializer ActiveModel::ArraySerializer, 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



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

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



114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/serial_spec/request_response/provide_matcher.rb', line 114

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



127
128
129
# File 'lib/serial_spec/request_response/provide_matcher.rb', line 127

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

#failure_message_when_negatedObject



131
132
133
# File 'lib/serial_spec/request_response/provide_matcher.rb', line 131

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

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

Returns:

  • (Boolean)


91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/serial_spec/request_response/provide_matcher.rb', line 91

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



77
78
79
80
81
82
83
84
85
# File 'lib/serial_spec/request_response/provide_matcher.rb', line 77

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



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

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

#strip_hypermedia(actual) ⇒ Object



87
88
89
# File 'lib/serial_spec/request_response/provide_matcher.rb', line 87

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