class RSpec::Rails::Matchers::BeANew
Public Class Methods
new(expected)
click to toggle source
# File lib/rspec/rails/matchers/be_a_new.rb, line 4 def initialize(expected) @expected = expected end
Public Instance Methods
failure_message_for_should()
click to toggle source
@api private
# 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)
click to toggle source
@api private
# 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)
click to toggle source
Use this to specify the specific attributes to match on the new record.
@example
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
# File lib/rspec/rails/matchers/be_a_new.rb, line 22 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 45 def attributes @attributes ||= {} end
attributes_match?(actual)
click to toggle source
# File lib/rspec/rails/matchers/be_a_new.rb, line 49 def attributes_match?(actual) attributes.stringify_keys.all? do |key, value| actual.attributes[key].eql?(value) end end
unmatched_attributes()
click to toggle source
# File lib/rspec/rails/matchers/be_a_new.rb, line 55 def unmatched_attributes attributes.stringify_keys.reject do |key, value| actual.attributes[key].eql?(value) end end