Created
May 15, 2009 20:15
-
-
Save stevenbristol/112414 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Shoulda # :nodoc: | |
module ActiveRecord # :nodoc: | |
module Matchers | |
# Ensures that the model is not valid if the given attribute is not | |
# present. | |
# | |
# Options: | |
# * <tt>with_message</tt> - value the test expects to find in | |
# <tt>errors.on(:attribute)</tt>. <tt>Regexp</tt> or <tt>String</tt>. | |
# Defaults to the translation for <tt>:blank</tt>. | |
# | |
# Examples: | |
# it { should validate_presence_of(:name) } | |
# it { should validate_presence_of(:name). | |
# with_message(/is not optional/) } | |
# | |
def validate_format_of(attr) | |
ValidateFormatOfMatcher.new(attr) | |
end | |
class ValidateFormatOfMatcher < ValidationMatcher # :nodoc: | |
def initialize(attribute) | |
super | |
end | |
def with_message(message) | |
@expected_message = message if message | |
self | |
end | |
def with(format) | |
raise "You may not call both with and not_with" if @fomat_to_fail | |
@fomat_to_pass = format | |
self | |
end | |
def not_with(format) | |
raise "You may not call both with and not_with" if @fomat_to_pass | |
@fomat_to_fail = format | |
self | |
end | |
def matches?(subject) | |
super(subject) | |
@expected_message ||= :blank | |
disallows_value_of(@fomat_to_fail, @expected_message) if @fomat_to_fail | |
allows_value_of(@fomat_to_pass, @expected_message) if @fomat_to_pass | |
end | |
def description | |
"#{@attribute} have a valid format" | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment