Sha256: 3c691d81fd126afa886b79af5c82bcf811c1201f6093e2999d1a6aa8ca422e3d

Contents?: true

Size: 1.81 KB

Versions: 6

Compression:

Stored size: 1.81 KB

Contents

module Spec
  module Rails
    module Matchers
      def validate_presence_of(attribute)
        return simple_matcher("model to validate the presence of #{attribute}") do |model|
          model.send("#{attribute}=", nil)
          !model.valid? && model.errors.invalid?(attribute)
        end
      end

      def validate_length_of(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
        
        return simple_matcher("model to validate the length of #{attribute} within #{min || 0} and #{max || 'Infinity'}") do |model|
          invalid = false
          if !min.nil? && min >= 1
            model.send("#{attribute}=", 'a' * (min - 1))

            invalid = !model.valid? && model.errors.invalid?(attribute)
          end
          
          if !max.nil?
            model.send("#{attribute}=", 'a' * (max + 1))

            invalid ||= !model.valid? && model.errors.invalid?(attribute)
          end
          invalid
        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

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
jnstq-acts_as_sms-0.1.0 test/vendor/plugins/rspec-on-rails-matchers/lib/spec/rails/matchers/validations.rb
jnstq-acts_as_sms-0.1.1 test/vendor/plugins/rspec-on-rails-matchers/lib/spec/rails/matchers/validations.rb
jnstq-acts_as_sms-0.1.3 test/vendor/plugins/rspec-on-rails-matchers/lib/spec/rails/matchers/validations.rb
jnstq-acts_as_sms-0.1.4 test/vendor/plugins/rspec-on-rails-matchers/lib/spec/rails/matchers/validations.rb
saturnflyer-rspec-rails-matchers-1.0.0 lib/spec/rails/matchers/validations.rb
saturnflyer-rspec-rails-matchers-1.0.1 lib/spec/rails/matchers/validations.rb