Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added rspec matcher for validates_format_of

Signed-off-by: Dan Croak <dcroak@thoughtbot.com>
  • Loading branch information...
commit 7d6aa5b6ecceef0bef3399bc328275a3700e06ac 1 parent 272ad4e
@stevenbristol stevenbristol authored croaky committed
View
1  lib/shoulda/active_record/matchers.rb
@@ -4,6 +4,7 @@
require 'shoulda/active_record/matchers/ensure_length_of_matcher'
require 'shoulda/active_record/matchers/ensure_inclusion_of_matcher'
require 'shoulda/active_record/matchers/validate_presence_of_matcher'
+require 'shoulda/active_record/matchers/validate_format_of_matcher'
require 'shoulda/active_record/matchers/validate_uniqueness_of_matcher'
require 'shoulda/active_record/matchers/validate_acceptance_of_matcher'
require 'shoulda/active_record/matchers/validate_numericality_of_matcher'
View
67 lib/shoulda/active_record/matchers/validate_format_of_matcher.rb
@@ -0,0 +1,67 @@
+module Shoulda # :nodoc:
+ module ActiveRecord # :nodoc:
+ module Matchers
+
+ # Ensures that the model is not valid if the given attribute is not
+ # formatted correctly.
+ #
+ # 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>.
+ # * <tt>with(string to test against)</tt>
+ # * <tt>not_with(string to test against)</tt>
+ #
+ # Examples:
+ # it { should validate_format_ofof(:name).
+ # with('12345').
+ # with_message(/is not optional/) }
+ # it { should validate_format_ofof(:name).
+ # not_with('12D45').
+ # 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(value)
+ raise "You may not call both with and not_with" if @value_to_fail
+ @value_to_pass = value
+ self
+ end
+
+
+ def not_with(value)
+ raise "You may not call both with and not_with" if @value_to_pass
+ @value_to_fail = value
+ self
+ end
+
+
+ def matches?(subject)
+ super(subject)
+ @expected_message ||= :blank
+ return disallows_value_of(@value_to_fail, @expected_message) if @value_to_fail
+ allows_value_of(@value_to_pass, @expected_message) if @value_to_pass
+ end
+
+ def description
+ "#{@attribute} have a valid format"
+ end
+
+ end
+
+ end
+ end
+end
View
1  lib/shoulda/active_record/matchers/validation_matcher.rb
@@ -18,6 +18,7 @@ def matches?(subject)
@subject = subject
false
end
+
private
View
39 test/matchers/active_record/validate_format_of_matcher_test.rb
@@ -0,0 +1,39 @@
+require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
+
+class ValidateFormatOfMatcherTest < ActiveSupport::TestCase # :nodoc:
+
+
+ context "a postal code" do
+ setup do
+ define_model :example, :attr => :string do
+ validates_format_of :attr, :with => /^\d{5}$/
+ end
+ @model = Example.new
+ end
+
+ should "be valid" do
+ assert_accepts validate_format_of(:attr).with('12345'), @model
+ end
+
+ should "not be valid with alpha in zip" do
+ assert_rejects validate_format_of(:attr).not_with('1234a'), @model, :message=>"is invalid"
+ end
+
+ should "not be valid with to few digits" do
+ assert_rejects validate_format_of(:attr).not_with('1234'), @model, :message=>"is invalid"
+ end
+
+ should "not be valid with to many digits" do
+ assert_rejects validate_format_of(:attr).not_with('123456'), @model, :message=>"is invalid"
+ end
+
+ should "raise error if you try to call both with and not_with" do
+ assert_raise RuntimeError do
+ validate_format_of(:attr).not_with('123456').with('12345')
+ end
+ end
+
+ end
+
+
+end
Please sign in to comment.
Something went wrong with that request. Please try again.