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
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)
        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.send("#{attribute}=", 'a' * (min - 1))

          invalid = !model.valid? && model.errors.invalid?(attribute)

          model.send("#{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
Use send as opposed to [] to make sure we catch virtual attributes
Use send as opposed to [] to make sure we catch virtual attributes


git-svn-id: 3569132a06236568bf2db79bd4cafefc04c0e5ed@4 11e6f158-dd3c-0410-a69b-4d739fb74d9d
