diff --git a/spec/shoulda/matchers/active_model/validate_presence_of_matcher_spec.rb b/spec/shoulda/matchers/active_model/validate_presence_of_matcher_spec.rb index 139988dc..59e3fdd4 100644 --- a/spec/shoulda/matchers/active_model/validate_presence_of_matcher_spec.rb +++ b/spec/shoulda/matchers/active_model/validate_presence_of_matcher_spec.rb @@ -29,8 +29,13 @@ describe Shoulda::Matchers::ActiveModel::ValidatePresenceOfMatcher do context 'an ActiveModel class without a presence validation' do it 'rejects' do - define_active_model_class('Example', :accessors => [:attr]).new. - should_not matcher + active_model.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 @@ -125,10 +130,12 @@ describe Shoulda::Matchers::ActiveModel::ValidatePresenceOfMatcher do end.new end + def active_model(&block) + define_active_model_class('Example', :accessors => [:attr], &block).new + end + def active_model_validating_presence - define_active_model_class('Example', :accessors => [:attr]) do - validates_presence_of :attr - end.new + active_model { validates_presence_of :attr } end def has_many_children(options = {}) diff --git a/spec/support/fail_with_message_matcher.rb b/spec/support/fail_with_message_matcher.rb new file mode 100644 index 00000000..73df6e79 --- /dev/null +++ b/spec/support/fail_with_message_matcher.rb @@ -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