Add failing spec for presence matcher
This commit is contained in:
parent
111dc5a2d4
commit
69a4a776b4
|
@ -29,8 +29,13 @@ describe Shoulda::Matchers::ActiveModel::ValidatePresenceOfMatcher do
|
||||||
|
|
||||||
context 'an ActiveModel class without a presence validation' do
|
context 'an ActiveModel class without a presence validation' do
|
||||||
it 'rejects' do
|
it 'rejects' do
|
||||||
define_active_model_class('Example', :accessors => [:attr]).new.
|
active_model.should_not matcher
|
||||||
should_not matcher
|
end
|
||||||
|
|
||||||
|
it 'provides the correct failure message' do
|
||||||
|
message = %{Expected errors to include "can't be blank" when attr is set to nil, got no errors}
|
||||||
|
|
||||||
|
expect { active_model.should matcher }.to fail_with_message(message)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -125,10 +130,12 @@ describe Shoulda::Matchers::ActiveModel::ValidatePresenceOfMatcher do
|
||||||
end.new
|
end.new
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def active_model(&block)
|
||||||
|
define_active_model_class('Example', :accessors => [:attr], &block).new
|
||||||
|
end
|
||||||
|
|
||||||
def active_model_validating_presence
|
def active_model_validating_presence
|
||||||
define_active_model_class('Example', :accessors => [:attr]) do
|
active_model { validates_presence_of :attr }
|
||||||
validates_presence_of :attr
|
|
||||||
end.new
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def has_many_children(options = {})
|
def has_many_children(options = {})
|
||||||
|
|
|
@ -0,0 +1,32 @@
|
||||||
|
RSpec::Matchers.define :fail_with_message do |expected|
|
||||||
|
match do |block|
|
||||||
|
@actual = nil
|
||||||
|
|
||||||
|
begin
|
||||||
|
block.call
|
||||||
|
rescue RSpec::Expectations::ExpectationNotMetError => ex
|
||||||
|
@actual = ex.message
|
||||||
|
end
|
||||||
|
|
||||||
|
@actual && @actual == expected
|
||||||
|
end
|
||||||
|
|
||||||
|
failure_message_for_should do
|
||||||
|
msg = "Expectation should have failed with message '#{expected}'"
|
||||||
|
|
||||||
|
if @actual
|
||||||
|
msg << ", actually failed with '#{@actual}'"
|
||||||
|
else
|
||||||
|
msg << ", but did not fail."
|
||||||
|
end
|
||||||
|
|
||||||
|
msg
|
||||||
|
end
|
||||||
|
|
||||||
|
failure_message_for_should_not do
|
||||||
|
msg = "Expectation should not have failed with message '#{expected}'"
|
||||||
|
msg << ", but did."
|
||||||
|
|
||||||
|
msg
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in New Issue