diff --git a/lib/shoulda/active_record/matchers.rb b/lib/shoulda/active_record/matchers.rb index bf9387f7..4ee03552 100644 --- a/lib/shoulda/active_record/matchers.rb +++ b/lib/shoulda/active_record/matchers.rb @@ -1,2 +1,3 @@ +require 'shoulda/active_record/helpers' require 'shoulda/active_record/matchers/attribute_matcher' require 'shoulda/active_record/matchers/association_matcher' diff --git a/lib/shoulda/active_record/matchers/attribute_matcher.rb b/lib/shoulda/active_record/matchers/attribute_matcher.rb index 1c6ddaa9..0d8f0df9 100644 --- a/lib/shoulda/active_record/matchers/attribute_matcher.rb +++ b/lib/shoulda/active_record/matchers/attribute_matcher.rb @@ -22,7 +22,10 @@ module Shoulda # :nodoc: def matches?(instance) @instance = instance - @expected_message ||= default_error_message(:invalid) + @expected_message ||= :invalid + if Symbol === @expected_message + @expected_message = default_error_message(@expected_message) + end @instance.send("#{@attribute}=", @value) !errors_match? end @@ -35,6 +38,17 @@ module Shoulda # :nodoc: "Expected #{expectation}, got errors: #{pretty_error_messages(@instance)}" end + def description + if @value.nil? + "allow #{@attribute} to be blank" + else + description = "have an attribute called #{@attribute}" + description << " accepting value #{@value.inspect}" + description << " without error #{@expected_message.inspect}" + description + end + end + private def errors_match? @@ -77,7 +91,7 @@ module Shoulda # :nodoc: end def allow_blank_for(attr) - AttributeMatcher.new.for(attr).accepting_value(nil) + AttributeMatcher.new.for(attr).accepting_value(nil).with_message(:blank) end end end diff --git a/lib/shoulda/matchers.rb b/lib/shoulda/matchers.rb new file mode 100644 index 00000000..c6eac0cb --- /dev/null +++ b/lib/shoulda/matchers.rb @@ -0,0 +1,9 @@ +require 'shoulda/active_record/matchers' + +module Thoughtbot + module Shoulda + module Matchers # :nodoc: + include ThoughtBot::Shoulda::ActiveRecord::Matchers + end + end +end diff --git a/test/other/active_record_matchers_test.rb b/test/other/active_record_matchers_test.rb index f8899655..939fb889 100644 --- a/test/other/active_record_matchers_test.rb +++ b/test/other/active_record_matchers_test.rb @@ -21,6 +21,16 @@ class ActiveRecordMatchersTest < Test::Unit::TestCase # :nodoc: end end + context "allow_blank_for(attr)" do + should "accept an attribute that allows a blank" do + assert_accepts allow_blank_for(:name), User.new + end + + should "reject an attribute that doesn't allow a blank" do + assert_rejects allow_blank_for(:title), Product.new + end + end + context "accept_value(value).for" do should "accept a good attribute value" do assert_accepts accept_value("good@example.com").for(:email), User.new