class RSpec::Rails::Matchers::BeANew

@api private

Matcher class for ‘be_a_new`. Should not be instantiated directly.

@see RSpec::Rails::Matchers#be_a_new

Public Class Methods

new(expected) click to toggle source

@private

# File lib/rspec/rails/matchers/be_a_new.rb, line 11
def initialize(expected)
  @expected = expected
end

Public Instance Methods

failure_message() click to toggle source

@private

# File lib/rspec/rails/matchers/be_a_new.rb, line 29
def failure_message
  [].tap do |message|
    unless actual.is_a?(expected) && actual.new_record?
      message << "expected #{actual.inspect} to be a new #{expected.inspect}"
    end
    unless attributes_match?(actual)
      describe_unmatched_attributes = surface_descriptions_in(unmatched_attributes)
      if unmatched_attributes.size > 1
        message << "attributes #{describe_unmatched_attributes.inspect} were not set on #{actual.inspect}"
      else
        message << "attribute #{describe_unmatched_attributes.inspect} was not set on #{actual.inspect}"
      end
    end
  end.join(' and ')
end
matches?(actual) click to toggle source

@private

# File lib/rspec/rails/matchers/be_a_new.rb, line 16
def matches?(actual)
  @actual = actual
  actual.is_a?(expected) && actual.new_record? && attributes_match?(actual)
end
with(expected_attributes) click to toggle source

@api public @see RSpec::Rails::Matchers#be_a_new

# File lib/rspec/rails/matchers/be_a_new.rb, line 23
def with(expected_attributes)
  attributes.merge!(expected_attributes)
  self
end

Private Instance Methods

attributes() click to toggle source
# File lib/rspec/rails/matchers/be_a_new.rb, line 47
def attributes
  @attributes ||= {}
end
attributes_match?(actual) click to toggle source
# File lib/rspec/rails/matchers/be_a_new.rb, line 51
def attributes_match?(actual)
  attributes.stringify_keys.all? do |key, value|
    values_match?(value, actual.attributes[key])
  end
end
unmatched_attributes() click to toggle source
# File lib/rspec/rails/matchers/be_a_new.rb, line 57
def unmatched_attributes
  attributes.stringify_keys.reject do |key, value|
    values_match?(value, actual.attributes[key])
  end
end