class RSpec::Resources::DSL::Matchers::JsonModelMatcher

Public Class Methods

new(expectation, attributes) click to toggle source
# File lib/rspec/resources/dsl/matchers/json_model_matcher.rb, line 8
def initialize(expectation, attributes)
  @expectation = expectation.is_a?(Hash) ? expectation.with_indifferent_access : expectation
  @attributes = attributes
end

Public Instance Methods

failure_message() click to toggle source
# File lib/rspec/resources/dsl/matchers/json_model_matcher.rb, line 28
def failure_message
  if @subject.is_a? Hash
    # we most likely check the result of a get operation
    "Expected returned attributes to match:\n#{formatted_fails}"
  else
    # we most likely check an update or create here
    "Expected model attributes to match:\n#{formatted_fails}"
  end
end
failure_message_when_negated() click to toggle source
# File lib/rspec/resources/dsl/matchers/json_model_matcher.rb, line 38
def failure_message_when_negated
  "Expected to not match:\n#{formatted_fails}"
end
matches?(subject) click to toggle source
# File lib/rspec/resources/dsl/matchers/json_model_matcher.rb, line 13
def matches?(subject)
  @subject = subject

  @fails = []

  @attributes.map(&:to_s).each do |a|
    exp = @expectation[a]
    subj = @subject[a]

    @fails.push(attribute: a, expectation: exp, subject: subj) if exp != subj
  end

  @fails.empty?
end

Private Instance Methods

formatted_fails() click to toggle source
# File lib/rspec/resources/dsl/matchers/json_model_matcher.rb, line 44
def formatted_fails
  @fails.map do |f|
    "  #{f[:attribute]} was '#{f[:subject]}' but expected to be '#{f[:expectation]}'"
  end.join("\n")
end