class SerialSpec::RequestResponse::ProvideMatcher::Provide

Constants

HYPERMEDIA_ATTRIBUTES

Attributes

actual[R]
as_serializer[R]
expected[R]

Public Class Methods

new(build_context, expected, options={}) click to toggle source
# 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

Public Instance Methods

==(actual)

when rspec asserts eq

Alias for: matches?
actual_to_hash(actual) click to toggle source
# File lib/serial_spec/request_response/provide_matcher.rb, line 39
def actual_to_hash(actual)
  actual.kind_of?(SerialSpec::ParsedBody) ? actual.execute : actual
end
collection_serializer() click to toggle source
# File lib/serial_spec/request_response/provide_matcher.rb, line 54
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_hash() click to toggle source

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

# File lib/serial_spec/request_response/provide_matcher.rb, line 64
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) click to toggle source
# File lib/serial_spec/request_response/provide_matcher.rb, line 110
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_message() click to toggle source
# File lib/serial_spec/request_response/provide_matcher.rb, line 123
def failure_message
  @failure_message || "error should TO  implement"
end
failure_message_when_negated() click to toggle source
# File lib/serial_spec/request_response/provide_matcher.rb, line 127
def failure_message_when_negated
  @failure_message || "error should not  TO implement"
end
matches?(actual) click to toggle source
# File lib/serial_spec/request_response/provide_matcher.rb, line 87
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
Also aliased as: ==
normalize_data(data) click to toggle source
# File lib/serial_spec/request_response/provide_matcher.rb, line 73
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_serializer() click to toggle source
# File lib/serial_spec/request_response/provide_matcher.rb, line 43
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) click to toggle source
# File lib/serial_spec/request_response/provide_matcher.rb, line 83
def strip_hypermedia(actual)
  (actual || {})
end