Changed matchers and added a lib/shoulda/matchers include so that matchers can be used outside shoulda

This commit is contained in:
Joe Ferris 2008-11-09 16:32:02 -05:00
parent 737f1f1f2f
commit ba0a96221d
4 changed files with 36 additions and 2 deletions

View File

@ -1,2 +1,3 @@
require 'shoulda/active_record/helpers'
require 'shoulda/active_record/matchers/attribute_matcher'
require 'shoulda/active_record/matchers/association_matcher'

View File

@ -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

9
lib/shoulda/matchers.rb Normal file
View File

@ -0,0 +1,9 @@
require 'shoulda/active_record/matchers'
module Thoughtbot
module Shoulda
module Matchers # :nodoc:
include ThoughtBot::Shoulda::ActiveRecord::Matchers
end
end
end

View File

@ -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