Sha256: db20a7c919613a8029d5abe1a7d1a2addbb39ddb48a6e0f7a766b07ec301066c

Contents?: true

Size: 1.84 KB

Versions: 38

Compression:

Stored size: 1.84 KB

Contents

require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')

class AllowValueMatcherTest < ActiveSupport::TestCase # :nodoc:

  context "an attribute with a format validation" do
    setup do
      define_model :example, :attr => :string do
        validates_format_of :attr, :with => /abc/
      end
      @model = Example.new
    end

    should "allow a good value" do
      assert_accepts allow_value("abcde").for(:attr), @model
    end
    
    should "not allow a bad value" do
      assert_rejects allow_value("xyz").for(:attr), @model
    end
  end

  context "an attribute with a format validation and a custom message" do
    setup do
      define_model :example, :attr => :string do
        validates_format_of :attr, :with => /abc/, :message => 'bad value'
      end
      @model = Example.new
    end

    should "allow a good value" do
      assert_accepts allow_value('abcde').for(:attr).with_message(/bad/),
                     @model
    end
    
    should "not allow a bad value" do
      assert_rejects allow_value('xyz').for(:attr).with_message(/bad/),
                     @model
    end
  end

  context "an attribute with several validations" do
    setup do
      define_model :example, :attr => :string do
        validates_presence_of     :attr
        validates_length_of       :attr, :within => 1..5
        validates_numericality_of :attr, :greater_than_or_equal_to => 1,
                                         :less_than_or_equal_to    => 50000
      end
      @model = Example.new
    end

    should "allow a good value" do
      assert_accepts allow_value("12345").for(:attr), @model
    end
    
    bad_values = [nil, "", "abc", "0", "50001", "123456"]
    bad_values.each do |value|
      should "not allow a bad value (#{value.inspect})" do
        assert_rejects allow_value(value).for(:attr), @model
      end
    end
  end

end

Version data entries

38 entries across 38 versions & 10 rubygems

Version Path
auser-poolparty-1.3.0 vendor/gems/shoulda/test/matchers/active_record/allow_value_matcher_test.rb
auser-poolparty-1.3.1 vendor/gems/shoulda/test/matchers/active_record/allow_value_matcher_test.rb
auser-poolparty-1.3.10 vendor/gems/shoulda/test/matchers/active_record/allow_value_matcher_test.rb
auser-poolparty-1.3.11 vendor/gems/shoulda/test/matchers/active_record/allow_value_matcher_test.rb
auser-poolparty-1.3.12 vendor/gems/shoulda/test/matchers/active_record/allow_value_matcher_test.rb
auser-poolparty-1.3.13 vendor/gems/shoulda/test/matchers/active_record/allow_value_matcher_test.rb
auser-poolparty-1.3.14 vendor/gems/shoulda/test/matchers/active_record/allow_value_matcher_test.rb
auser-poolparty-1.3.15 vendor/gems/shoulda/test/matchers/active_record/allow_value_matcher_test.rb
auser-poolparty-1.3.16 vendor/gems/shoulda/test/matchers/active_record/allow_value_matcher_test.rb
auser-poolparty-1.3.17 vendor/gems/shoulda/test/matchers/active_record/allow_value_matcher_test.rb
auser-poolparty-1.3.2 vendor/gems/shoulda/test/matchers/active_record/allow_value_matcher_test.rb
auser-poolparty-1.3.3 vendor/gems/shoulda/test/matchers/active_record/allow_value_matcher_test.rb
auser-poolparty-1.3.4 vendor/gems/shoulda/test/matchers/active_record/allow_value_matcher_test.rb
auser-poolparty-1.3.5 vendor/gems/shoulda/test/matchers/active_record/allow_value_matcher_test.rb
auser-poolparty-1.3.6 vendor/gems/shoulda/test/matchers/active_record/allow_value_matcher_test.rb
auser-poolparty-1.3.7 vendor/gems/shoulda/test/matchers/active_record/allow_value_matcher_test.rb
auser-poolparty-1.3.8 vendor/gems/shoulda/test/matchers/active_record/allow_value_matcher_test.rb
fairchild-poolparty-1.3.17 vendor/gems/shoulda/test/matchers/active_record/allow_value_matcher_test.rb
fairchild-poolparty-1.3.5 vendor/gems/shoulda/test/matchers/active_record/allow_value_matcher_test.rb
iGEL-shoulda-2.10.2 test/matchers/active_record/allow_value_matcher_test.rb