File: validates_format_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 (34 lines) | stat: -rw-r--r-- 1,195 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
require File.expand_path(File.dirname(__FILE__) + '/../test_helper')

module Functional
  class ValidatesFormatOfTest < Test::Unit::TestCase
    test "given invalid name format, when validated, then error is in the objects error collection" do
      klass = Class.new do
        include Validatable
        attr_accessor :name
        validates_format_of :name, :with => /.+/
      end
      instance = klass.new
      instance.valid?
      assert_equal "is invalid", instance.errors.on(:name)
    end

    test "given invalid name format and nil name, when validated, then error is in the objects error collection" do
      klass = Class.new do
        include Validatable
        attr_accessor :name
        validates_format_of :name, :with => /.+/, :if => Proc.new { !name.nil? }
      end
      assert_equal true, klass.new.valid?
    end
    
    test "given invalid name format and a name, when validated, then error is in the objects error collection" do
      klass = Class.new do
        include Validatable
        attr_accessor :name
        validates_format_of :name, :with => /.+/, :if => Proc.new { name.nil? }
      end
      assert_equal false, klass.new.valid?
    end
  end
end