Permalink
Browse files

Start work on validate_presence_of matcher.

  • Loading branch information...
1 parent ea1466b commit 0776602c2dc2cc02efa38a14c8614faff35ff3d1 @nmerouze nmerouze committed Jul 28, 2009
@@ -12,6 +12,8 @@
# Add locale
dir = File.dirname(__FILE__)
Remarkable.add_locale File.join(dir, '..', 'locales', 'en.yml')
+
+require File.join(dir, 'remarkable_mongomapper', 'base')
# Add matchers
Dir[File.join(dir, 'remarkable_mongomapper', 'matchers', '*.rb')].each do |file|
@@ -0,0 +1,6 @@
+module Remarkable
+ module MongoMapper
+ class Base < Remarkable::Base
+ end
+ end
+end
@@ -1,7 +1,7 @@
module Remarkable
module MongoMapper
module Matchers
- class HaveKeyMatcher < Remarkable::Base #:nodoc:
+ class HaveKeyMatcher < Remarkable::MongoMapper::Base #:nodoc:
arguments :collection => :keys, :as => :key
@@ -0,0 +1,25 @@
+module Remarkable
+ module MongoMapper
+ module Matchers
+ class ValidatePresenceOfMatcher < Remarkable::MongoMapper::Base #:nodoc:
+
+ arguments :collection => :keys, :as => :key
+
+ collection_assertions :allow_nil?
+
+ protected
+
+ def allow_nil?
+ @subject.send("#{@key}=", nil)
+ !@subject.valid? && !@subject.errors.on(@key).blank?
+ end
+
+ end
+
+ def validate_presence_of(*args, &block)
+ ValidatePresenceOfMatcher.new(*args, &block).spec(self)
+ end
+
+ end
+ end
+end
View
@@ -4,4 +4,9 @@ en:
have_key:
description: "have key(s) {{keys}}"
expectations:
- has_key: "{{subject_name}} to have key named {{key}} with type {{type}}"
+ has_key: "{{subject_name}} to have key named {{key}} with type {{type}}"
+
+ validate_presence_of:
+ description: "require {{keys}} to be set"
+ expectations:
+ allow_nil: "{{subject_name}} to require {{key}} to be set"
@@ -0,0 +1,33 @@
+require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
+
+describe 'validate_presence_of' do
+ subject do
+ Article.new
+ end
+
+ describe 'messages' do
+
+ it 'should contain a description' do
+ matcher = validate_presence_of(:title, :body)
+ matcher.description.should == 'require title and body to be set'
+ end
+
+ it 'should set allow_nil? message' do
+ matcher = validate_presence_of(:body)
+ matcher.matches?(subject)
+ matcher.failure_message.should == 'Expected Article to require body to be set'
+ matcher.negative_failure_message.should == 'Did not expect Article to require body to be set'
+ end
+
+ end
+
+ describe 'matchers' do
+ it { should validate_presence_of(:title) }
+ it { should_not validate_presence_of(:body) }
+ end
+
+ describe 'macros' do
+ should_validate_presence_of(:title)
+ should_not_validate_presence_of(:body)
+ end
+end
View
@@ -1,6 +1,6 @@
class Article
include MongoMapper::Document
- key :title, String
+ key :title, String, :required => true
key :body, String
end

0 comments on commit 0776602

Please sign in to comment.