File: validates_numericality_of_test.rb

package info (click to toggle)
ruby-validatable 1.6.7-11
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 284 kB
  • sloc: ruby: 1,636; makefile: 10
file content (57 lines) | stat: -rw-r--r-- 1,777 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
require File.expand_path(File.dirname(__FILE__) + '/../test_helper')

module Functional
  class ValidatesNumericalityOfTest < Test::Unit::TestCase
    test "when validating numericality and the value is nil an error should exist on the instance" do
      klass = Class.new do
        include Validatable
        attr_accessor :nothing
        validates_numericality_of :nothing
      end
      instance = klass.new
      instance.valid?
      assert_equal "must be a number", instance.errors.on(:nothing)
    end

    test "when validating numericality and the value has a non numeric character an error should exist on the instance" do
      klass = Class.new do
        include Validatable
        validates_numericality_of :some_string
        
        def some_string
          "some_string"
        end
      end
      instance = klass.new
      instance.valid?
      assert_equal "must be a number", instance.errors.on(:some_string)
    end
    
    test "when validating a number no error will be in the instance" do
      klass = Class.new do
        include Validatable
        validates_numericality_of :valid_number
        
        def valid_number
          1.23
        end
      end
      instance = klass.new
      instance.valid?
      assert_equal nil, instance.errors.on(:valid_number)
    end
    
    test "when validating an integer and the value is a decimal an error should exist on the instance" do
      klass = Class.new do
        include Validatable
        validates_numericality_of :valid_number, :only_integer => true
        attr_accessor :valid_number
      end
      
      instance = klass.new
      instance.valid_number = 1.23
      instance.valid?
      assert_equal "must be a number", instance.errors.on(:valid_number)
    end
  end
end