Permalink
Browse files

Add validate_confirmation_of matcher.

  • Loading branch information...
nmerouze committed Jul 31, 2009
1 parent 0248722 commit 8dfabc6d9627e94b8fea970e61c8cd2d8a89b7fb
@@ -0,0 +1,44 @@
+module Remarkable
+ module MongoMapper
+ module Matchers
+ class ValidateConfirmationOfMatcher < Remarkable::MongoMapper::Base #:nodoc:
+ arguments :collection => :attributes, :as => :attribute
+
+ optional :message
+ collection_assertions :responds_to_confirmation?, :confirms?
+
+ default_options :message => "doesn't match confirmation"
+
+ protected
+
+ def responds_to_confirmation?
+ @subject.respond_to?(:"#{@attribute}_confirmation=")
+ end
+
+ def confirms?
+ @subject.send(:"#{@attribute}_confirmation=", 'something')
+ bad?('different')
+ end
+
+ end
+
+ # Ensures that the model cannot be saved if one of the attributes is not confirmed.
+ #
+ # == Options
+ #
+ # * <tt>:message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
+ # Regexp, string or symbol. Default = "doesn't match confirmation"
+ #
+ # == Examples
+ #
+ # should_validate_confirmation_of :email, :password
+ #
+ # it { should validate_confirmation_of(:email, :password) }
+ #
+ def validate_confirmation_of(*attributes, &block)
+ ValidateConfirmationOfMatcher.new(*attributes, &block).spec(self)
+ end
+
+ end
+ end
+end
View
@@ -35,6 +35,12 @@ en:
description: "have key(s) {{attributes}} with type {{type}}"
expectations:
has_key: "{{subject_name}} to have key named {{attribute}} with type {{type}}"
+
+ validate_confirmation_of:
+ description: "require {{attributes}} to be confirmed"
+ expectations:
+ responds_to_confirmation: "{{subject_name}} instance responds to {{attribute}}_confirmation"
+ confirms: "{{subject_name}} to be valid only when {{attribute}} is confirmed"
validate_presence_of:
description: "require {{attributes}} to be set"
@@ -0,0 +1,64 @@
+require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
+
+describe 'validate_confirmation_of' do
+ include ModelBuilder
+
+ # Defines a model, create a validation and returns a raw matcher
+ def define_and_validate(options={})
+ @model = define_model :person do
+ include MongoMapper::Document
+
+ key :name, String
+ key :email, String
+ key :age, String
+
+ validates_confirmation_of :name, :email, options
+ end
+
+ validate_confirmation_of(:name, :email)
+ end
+
+ describe 'messages' do
+ before(:each){ @matcher = define_and_validate }
+
+ it 'should contain a description' do
+ @matcher.description.should == 'require name and email to be confirmed'
+ end
+
+ it 'should set responds_to_confirmation? message' do
+ @matcher = validate_confirmation_of(:age)
+ @matcher.matches?(@model)
+ @matcher.failure_message.should == 'Expected Person instance responds to age_confirmation'
+ end
+
+ it 'should set confirms? message' do
+ @model.instance_eval{ def age_confirmation=(*args); end }
+ @matcher = validate_confirmation_of(:age)
+ @matcher.matches?(@model)
+ @matcher.failure_message.should == 'Expected Person to be valid only when age is confirmed'
+ end
+
+ end
+
+ describe 'matchers' do
+
+ describe 'without options' do
+ before(:each){ define_and_validate }
+
+ it { should validate_confirmation_of(:name) }
+ it { should validate_confirmation_of(:name, :email) }
+ it { should_not validate_confirmation_of(:name, :age) }
+ end
+
+ create_message_specs(self)
+ end
+
+ describe 'macros' do
+ before(:each){ define_and_validate }
+
+ should_validate_confirmation_of :name
+ should_validate_confirmation_of :name, :email
+ should_not_validate_confirmation_of :name, :age
+ end
+
+end

0 comments on commit 8dfabc6

Please sign in to comment.