Skip to content
Browse files

Fix broken validate_length_of matcher. Include spec.

  • Loading branch information...
1 parent f9376f5 commit d60eea1bd43184bf82965f2a920a59a017e2026c Matt Pelletier committed Feb 20, 2012
Showing with 40 additions and 22 deletions.
  1. +23 −21 lib/rspec_on_rails_matchers/validations.rb
  2. +15 −0 spec/rspec-on-rails-matchers/validations_spec.rb
  3. +2 −1 spec/spec_helper.rb
View
44 lib/rspec_on_rails_matchers/validations.rb
@@ -10,30 +10,32 @@
end
RSpec::Matchers.define :validate_length_of do |attribute, options|
- if options.has_key? :within
- min = options[:within].first
- max = options[:within].last
- elsif options.has_key? :is
- min = options[:is]
- max = min
- elsif options.has_key? :minimum
- min = options[:minimum]
- elsif options.has_key? :maximum
- max = options[:maximum]
- end
+ match do |object|
+ if options.has_key? :within
+ min = options[:within].first
+ max = options[:within].last
+ elsif options.has_key? :is
+ min = options[:is]
+ max = min
+ elsif options.has_key? :minimum
+ min = options[:minimum]
+ elsif options.has_key? :maximum
+ max = options[:maximum]
+ end
- invalid = false
- if !min.nil? && min >= 1
- object.send("#{attribute}=", 'a' * (min - 1))
- invalid = !object.valid? && object.errors[attribute].any?
- end
+ invalid = false
+ if !min.nil? && min >= 1
+ object.send("#{attribute}=", 'a' * (min - 1))
+ invalid = !object.valid? && object.errors[attribute].any?
+ end
- if !max.nil?
- object.send("#{attribute}=", 'a' * (max + 1))
- invalid ||= !object.valid? && object.errors[attribute].any?
- end
+ if !max.nil?
+ object.send("#{attribute}=", 'a' * (max + 1))
+ invalid ||= !object.valid? && object.errors[attribute].any?
+ end
- invalid
+ invalid
+ end
end
RSpec::Matchers.define :validate_uniqueness_of do |attribute|
View
15 spec/rspec-on-rails-matchers/validations_spec.rb
@@ -38,6 +38,21 @@
end
end
+ describe "validate_length_of" do
+ before do
+ @comment = Comment.new(:title => "x", :post => Post.new)
+ end
+
+ it "should be true if the length is within the given bounds" do
+ matcher = @example.validate_length_of(:title, :within => 1..10)
+ matcher.matches?(@comment).should be_true
+ end
+
+ it "should not be true if the length is not within the given bounds" do
+ @example.validate_length_of(:title, :within => 3..5).matches?(@comment).should be_false
+ end
+ end
+
describe "validate_boolean_of" do
before do
@boolean = Boolean.new
View
3 spec/spec_helper.rb
@@ -9,8 +9,9 @@ class Comment < ActiveRecord::Base
belongs_to :commentable, :polymorphic => true
validates_presence_of :post
+ validates_length_of :title, :within => 1..10
- attr_accessor :an_attr_accessor
+ attr_accessor :an_attr_accessor, :title
end
class Image < ActiveRecord::Base

0 comments on commit d60eea1

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