Sha256: 9215b1e720e4258a5f925897a8c80d8adabbc9313a64d603f5dead77f269598f

Contents?: true

Size: 1.86 KB

Versions: 4

Compression:

Stored size: 1.86 KB

Contents

require './test/helper'

class ValidateAttachmentPresenceMatcherTest < Test::Unit::TestCase
  context "validate_attachment_presence" do
    setup do
      reset_table("dummies") do |d|
        d.string :avatar_file_name
      end
      @dummy_class = reset_class "Dummy"
      @dummy_class.has_attached_file :avatar
      @matcher     = self.class.validate_attachment_presence(:avatar)
    end

    context "given a class with no validation" do
      should_reject_dummy_class
    end

    context "given a class with a matching validation" do
      setup do
        @dummy_class.validates_attachment_presence :avatar
      end

      should_accept_dummy_class
    end

    context "given an instance with other attachment validations" do
      setup do
        reset_table("dummies") do |d|
          d.string :avatar_file_name
          d.string :avatar_content_type
        end

        @dummy_class.class_eval do
          validates_attachment_presence :avatar
          validates_attachment_content_type :avatar, :content_type => 'image/gif'
        end

        @dummy = @dummy_class.new
        @matcher = self.class.validate_attachment_presence(:avatar)
      end

      should "it should validate properly" do
        @dummy.avatar = File.new fixture_file('5k.png')
        assert_accepts @matcher, @dummy
      end
    end

    context "using an :if to control the validation" do
      setup do
        @dummy_class.class_eval do
          validates_attachment_presence :avatar, :if => :go
          attr_accessor :go
        end
        @dummy = @dummy_class.new
        @dummy.avatar = nil
      end

      should "run the validation if the control is true" do
        @dummy.go = true
        assert_accepts @matcher, @dummy
      end

      should "not run the validation if the control is false" do
        @dummy.go = false
        assert_rejects @matcher, @dummy
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
paperclip-3.5.4 test/matchers/validate_attachment_presence_matcher_test.rb
paperclip-3.5.3 test/matchers/validate_attachment_presence_matcher_test.rb
paperclip-3.5.2 test/matchers/validate_attachment_presence_matcher_test.rb
paperclip-3.5.1 test/matchers/validate_attachment_presence_matcher_test.rb