Class: RSpec::Rails::Matchers::BeANew

Inherits:
Matchers::BuiltIn::BaseMatcher
  • Object
show all
Defined in:
lib/rspec/rails/matchers/be_a_new.rb

Instance Method Summary collapse

Constructor Details

#initialize(expected) ⇒ BeANew

Returns a new instance of BeANew.



4
5
6
# File 'lib/rspec/rails/matchers/be_a_new.rb', line 4

def initialize(expected)
  @expected = expected
end

Instance Method Details

#failure_message_for_shouldObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/rspec/rails/matchers/be_a_new.rb', line 28

def failure_message_for_should
  [].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)
      if unmatched_attributes.size > 1
        message << "attributes #{unmatched_attributes.inspect} were not set on #{actual.inspect}"
      else
        message << "attribute #{unmatched_attributes.inspect} was not set on #{actual.inspect}"
      end
    end
  end.join(' and ')
end

#matches?(actual) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


9
10
11
12
# File 'lib/rspec/rails/matchers/be_a_new.rb', line 9

def matches?(actual)
  @actual = actual
  actual.is_a?(expected) && actual.new_record? && attributes_match?(actual)
end

#with(expected_attributes) ⇒ Object

Use this to specify the specific attributes to match on the new record.

Examples:


it "assigns a new Thing with the submitted attributes" do
  post :create, :thing => { :name => "Illegal Value" }
  assigns(:thing).should be_a_new(Thing).with(:name => nil)
end


22
23
24
25
# File 'lib/rspec/rails/matchers/be_a_new.rb', line 22

def with(expected_attributes)
  attributes.merge!(expected_attributes)
  self
end