Skip to content
Browse files

Updated rspec matchers

  • Loading branch information...
1 parent 02a6c08 commit fe5740b1677c1960b62e723100e0c2e74cdbc223 @hpoydar hpoydar committed May 12, 2009
View
10 README.md
@@ -53,23 +53,26 @@ For testing or spec'ing, use these helpers to setup and teardown a test database
CouchRestRails::Tests.setup
CouchRestRails::Tests.teardown
+
+There are also some simple matchers you can can use to spec validations. See `spec/lib/matchers`.
### CouchRestRails document model
-For models, inherit from CouchRestRails::Document, which hooks up CouchRest::ExtendedDocument to your CouchDB backend and includes the CouchRest::Validatation module:
+For models, inherit from CouchRestRails::Document, which hooks up CouchRest::ExtendedDocument to your CouchDB backend and includes the [Validatable](http://validatable.rubyforge.org/) module:
class YourCouchDocument < CouchRestRails::Document
property :email
property :question
property :answer
+ property :rating
timestamps!
view_by :email
- validates_present :question
- validates_format :email, :as => :email_address
+ validates_presence_of :question
+ validates_numericality_of :rating
...
@@ -101,7 +104,6 @@ To run the test suite, you'll need rspec installed with rspec-rails library enab
## TODO
-* Flesh out rspec matchers
* A persistent connection object? Keep-alive?
* Hook into Rails logger to display times for CouchDB operations
* Mechanism for better view testing?
View
5 init.rb
@@ -1,4 +1,7 @@
require 'couch_rest_rails'
+require 'validatable'
require 'spec/rails/matchers/couch_document_validations'
-config.gem 'couchrest'
+config.gem 'couchrest'
+config.gem 'validatable'
+config.gem 'json'
View
2 lib/couch_rest_rails/document.rb
@@ -1,7 +1,7 @@
module CouchRestRails
class Document < CouchRest::ExtendedDocument
use_database COUCHDB_SERVER.default_database
- include CouchRest::Validation
+ include Validatable
end
end
View
36 lib/spec/rails/matchers/couch_document_validations.rb
@@ -2,10 +2,40 @@ module Spec
module Rails
module Matchers
- def validate_present(attribute)
- return simple_matcher("model to validate_present :#{attribute}") do |model|
+ def validate_couchdb_document_format_of(attribute, options)
+ return simple_matcher("document model to validate format of :#{attribute} with #{options[:with]}") do |model|
model.send("#{attribute}=", nil)
- !model.valid? && model.errors[attribute]
+ !model.valid? && model.errors.on(attribute)
+ end
+ end
+
+ def validate_couchdb_document_length_of(attribute, options)
+ return simple_matcher("document model to validate length of :#{attribute} within
+ #{options[:maximum] || 0} to #{options[:minimum] || 'infinity'}") do |model|
+ if options[:within]
+ model.send("#{attribute}=", 'x' * (options[:within].last + 1))
+ else
+ if options[:maximum]
+ model.send("#{attribute}=", 'x' * (options[:maximum] + 1))
+ else
+ model.send("#{attribute}=", 'x' * (options[:minimum] - 1))
+ end
+ end
+ !model.valid? && model.errors.on(attribute)
+ end
+ end
+
+ def validate_couchdb_document_presence_of(attribute)
+ return simple_matcher("document model to validate presence of :#{attribute}") do |model|
+ model.send("#{attribute}=", nil)
+ !model.valid? && model.errors.on(attribute)
+ end
+ end
+
+ def validate_couchdb_document_numericality_of(attribute)
+ return simple_matcher("document model to validate numericality of :#{attribute}") do |model|
+ model.send("#{attribute}=", 'x')
+ !model.valid? && model.errors.on(attribute)
end
end
View
32 spec/matchers/couch_document_validations_spec.rb
@@ -7,17 +7,37 @@
before :all do
class CouchFoo < CouchRestRails::Document
- property :question
- property :answer
+ property :something_present
+ property :something_long
+ property :something_formatted
+ property :something_numeric
- validates_present :question
-
+ validates_presence_of :something_present
+ validates_length_of :something_long, :minimum => 10, :maximum => 50
+ validates_format_of :something_formatted, :with => /[A-Z]{3}-[0-9]{3}/
+ validates_numericality_of :something_numeric
+
end
@couch_foo = CouchFoo.new
end
- it "should have a matcher for validates_present" do
- @couch_foo.should validate_present(:question)
+ # Use lengthy matcher names so as not to interfere with
+ # rspec-on-rails-matchers plugin if present
+
+ it "should have a matcher for validates_presence_of" do
+ @couch_foo.should validate_couchdb_document_presence_of(:something_present)
+ end
+
+ it "should have a matcher for validates_numericality_of" do
+ @couch_foo.should validate_couchdb_document_numericality_of(:something_numeric)
+ end
+
+ it "should have a matcher for validates_format_of" do
+ @couch_foo.should validate_couchdb_document_format_of(:something_formatted, :with => /[A-Z]{3}-[0-9]{3}/)
+ end
+
+ it "should have a matcher for validates_length_of" do
+ @couch_foo.should validate_couchdb_document_length_of(:something_long, :minimum => 10, :maximum => 50)
end
end

0 comments on commit fe5740b

Please sign in to comment.
Something went wrong with that request. Please try again.