Permalink
Browse files

Initial import

git-svn-id: https://rspec-on-rails-matchers.googlecode.com/svn/trunk@2 11e6f158-dd3c-0410-a69b-4d739fb74d9d
  • Loading branch information...
1 parent bf14172 commit 464a0975f31b12d5fe698e5c8c3d983c263f6b1d joshknowles committed Dec 10, 2007
Showing with 114 additions and 0 deletions.
  1. +20 −0 MIT-LICENSE
  2. +18 −0 README
  3. +2 −0 init.rb
  4. +33 −0 lib/spec/rails/matchers/associatons.rb
  5. +41 −0 lib/spec/rails/matchers/validations.rb
View
20 MIT-LICENSE
@@ -0,0 +1,20 @@
+Copyright (c) 2007 Josh Knowles
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
View
18 README
@@ -0,0 +1,18 @@
+rspec-on-rails-matchers
+=======================
+
+Overview
+--------
+
+Adds the following RSpec matchers:
+
+ * blah
+ * blah
+ * blah
+
+Authors
+-------
+
+ * Josh Knowles <joshknowles@gmail.com>
+
+Copyright (c) 2007 Josh Knowles, released under the MIT license
View
2 init.rb
@@ -0,0 +1,2 @@
+require 'spec/rails/matchers/associations'
+require 'spec/rails/matchers/validations'
View
33 lib/spec/rails/matchers/associatons.rb
@@ -0,0 +1,33 @@
+module Spec
+ module Rails
+ module Matchers
+ def belong_to(association)
+ return simple_matcher("model to belong to #{association}") do |model|
+ model = model.class if model.is_a? ActiveRecord::Base
+ model.reflect_on_all_associations(:belongs_to).find { |a| a.name == association }
+ end
+ end
+
+ def have_many(association)
+ return simple_matcher("model to have many #{association}") do |model|
+ model = model.class if model.is_a? ActiveRecord::Base
+ model.reflect_on_all_associations(:has_many).find { |a| a.name == association }
+ end
+ end
+
+ def have_one(association)
+ return simple_matcher("model to have one #{association}") do |model|
+ model = model.class if model.is_a? ActiveRecord::Base
+ model.reflect_on_all_associations(:has_one).find { |a| a.name == association }
+ end
+ end
+
+ def have_and_belong_to_many(association)
+ return simple_matcher("model to have and belong to many #{association}") do |model|
+ model = model.class if model.is_a? ActiveRecord::Base
+ model.reflect_on_all_associations(:has_and_belongs_to_many).find { |a| a.name == association }
+ end
+ end
+ end
+ end
+end
View
41 lib/spec/rails/matchers/validations.rb
@@ -0,0 +1,41 @@
+module Spec
+ module Rails
+ module Matchers
+ def validate_presence_of(attribute)
+ return simple_matcher("model to validate the presence of #{attribute}") do |model|
+ model[attribute] = nil
+ !model.valid? && model.errors.invalid?(attribute)
+ end
+ end
+
+ def validate_length_of(attribute, options)
+ min = options[:between].first
+ max = options[:between].last
+
+ return simple_matcher("model to validate the length of #{attribute} between #{min} and #{max}") do |model|
+ model[attribute] = 'a' * (min - 1)
+
+ invalid = !model.valid? && model.errors.invalid?(attribute)
+
+ model[attribute] = 'a' * (max + 1)
+
+ invalid && !model.valid? && model.errors.invalid?(attribute)
+ end
+ end
+
+ def validate_uniqueness_of(attribute)
+ return simple_matcher("model to validate the uniqueness of #{attribute}") do |model|
+ model.class.stub!(:find).and_return(true)
+ !model.valid? && model.errors.invalid?(attribute)
+ end
+ end
+
+ def validate_confirmation_of(attribute)
+ return simple_matcher("model to validate the confirmation of #{attribute}") do |model|
+ model.send("#{attribute}_confirmation=", 'asdf')
+ !model.valid? && model.errors.invalid?(attribute)
+ end
+ end
+ end
+ end
+end

0 comments on commit 464a097

Please sign in to comment.