Changed matchers and added a lib/shoulda/matchers include so that matchers can be used outside shoulda
This commit is contained in:
parent
737f1f1f2f
commit
ba0a96221d
|
@ -1,2 +1,3 @@
|
|||
require 'shoulda/active_record/helpers'
|
||||
require 'shoulda/active_record/matchers/attribute_matcher'
|
||||
require 'shoulda/active_record/matchers/association_matcher'
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -0,0 +1,9 @@
|
|||
require 'shoulda/active_record/matchers'
|
||||
|
||||
module Thoughtbot
|
||||
module Shoulda
|
||||
module Matchers # :nodoc:
|
||||
include ThoughtBot::Shoulda::ActiveRecord::Matchers
|
||||
end
|
||||
end
|
||||
end
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue