remarkable_activerecord 3.0.1 → 3.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +46 -46
- data/LICENSE +1 -1
- data/README +64 -64
- data/lib/remarkable_activerecord.rb +1 -1
- data/lib/remarkable_activerecord/base.rb +40 -40
- data/lib/remarkable_activerecord/human_names.rb +24 -24
- data/lib/remarkable_activerecord/matchers/allow_mass_assignment_of_matcher.rb +14 -14
- data/lib/remarkable_activerecord/matchers/allow_values_for_matcher.rb +70 -70
- data/lib/remarkable_activerecord/matchers/association_matcher.rb +197 -197
- data/lib/remarkable_activerecord/matchers/have_column_matcher.rb +29 -29
- data/lib/remarkable_activerecord/matchers/have_index_matcher.rb +20 -20
- data/lib/remarkable_activerecord/matchers/have_readonly_attributes_matcher.rb +7 -7
- data/lib/remarkable_activerecord/matchers/have_scope_matcher.rb +34 -34
- data/lib/remarkable_activerecord/matchers/validate_acceptance_of_matcher.rb +37 -37
- data/lib/remarkable_activerecord/matchers/validate_associated_matcher.rb +75 -75
- data/lib/remarkable_activerecord/matchers/validate_confirmation_of_matcher.rb +44 -44
- data/lib/remarkable_activerecord/matchers/validate_exclusion_of_matcher.rb +17 -17
- data/lib/remarkable_activerecord/matchers/validate_inclusion_of_matcher.rb +20 -20
- data/lib/remarkable_activerecord/matchers/validate_numericality_of_matcher.rb +14 -14
- data/lib/remarkable_activerecord/matchers/validate_uniqueness_of_matcher.rb +80 -61
- data/locale/en.yml +253 -253
- data/spec/allow_mass_assignment_of_matcher_spec.rb +50 -50
- data/spec/allow_values_for_matcher_spec.rb +45 -45
- data/spec/association_matcher_spec.rb +612 -612
- data/spec/have_column_matcher_spec.rb +67 -67
- data/spec/have_index_matcher_spec.rb +61 -61
- data/spec/have_readonly_attributes_matcher_spec.rb +40 -40
- data/spec/have_scope_matcher_spec.rb +60 -60
- data/spec/model_builder.rb +101 -101
- data/spec/spec_helper.rb +25 -25
- data/spec/validate_acceptance_of_matcher_spec.rb +64 -64
- data/spec/validate_associated_matcher_spec.rb +118 -118
- data/spec/validate_confirmation_of_matcher_spec.rb +54 -54
- data/spec/validate_exclusion_of_matcher_spec.rb +76 -76
- data/spec/validate_inclusion_of_matcher_spec.rb +72 -72
- data/spec/validate_numericality_of_matcher_spec.rb +100 -100
- data/spec/validate_presence_of_matcher_spec.rb +40 -40
- data/spec/validate_uniqueness_of_matcher_spec.rb +158 -139
- metadata +3 -3
@@ -1,58 +1,58 @@
|
|
1
1
|
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
2
|
|
3
|
-
describe 'validate_confirmation_of' do
|
3
|
+
describe 'validate_confirmation_of' do
|
4
4
|
include ModelBuilder
|
5
|
-
|
6
|
-
# Defines a model, create a validation and returns a raw matcher
|
7
|
-
def define_and_validate(options={})
|
8
|
-
@model = define_model :user, :name => :string, :email => :string, :age => :string do
|
9
|
-
validates_confirmation_of :name, :email, options
|
10
|
-
end
|
11
|
-
|
12
|
-
validate_confirmation_of(:name, :email)
|
13
|
-
end
|
14
|
-
|
15
|
-
describe 'messages' do
|
16
|
-
before(:each){ @matcher = define_and_validate }
|
17
|
-
|
18
|
-
it 'should contain a description' do
|
19
|
-
@matcher.description.should == 'require name and email to be confirmed'
|
20
|
-
end
|
21
|
-
|
22
|
-
it 'should set responds_to_confirmation? message' do
|
23
|
-
@matcher = validate_confirmation_of(:age)
|
24
|
-
@matcher.matches?(@model)
|
25
|
-
@matcher.failure_message.should == 'Expected User instance responds to age_confirmation'
|
26
|
-
end
|
27
|
-
|
28
|
-
it 'should set confirms? message' do
|
29
|
-
@model.instance_eval{ def age_confirmation=(*args); end }
|
30
|
-
@matcher = validate_confirmation_of(:age)
|
31
|
-
@matcher.matches?(@model)
|
32
|
-
@matcher.failure_message.should == 'Expected User to be valid only when age is confirmed'
|
33
|
-
end
|
34
|
-
|
35
|
-
end
|
36
|
-
|
37
|
-
describe 'matchers' do
|
38
|
-
|
39
|
-
describe 'without options' do
|
40
|
-
before(:each){ define_and_validate }
|
41
|
-
|
42
|
-
it { should validate_confirmation_of(:name) }
|
43
|
-
it { should validate_confirmation_of(:name, :email) }
|
44
|
-
it { should_not validate_confirmation_of(:name, :age) }
|
45
|
-
end
|
46
|
-
|
47
|
-
create_message_specs(self)
|
48
|
-
end
|
49
|
-
|
50
|
-
describe 'macros' do
|
51
|
-
before(:each){ define_and_validate }
|
52
|
-
|
53
|
-
should_validate_confirmation_of :name
|
54
|
-
should_validate_confirmation_of :name, :email
|
55
|
-
should_not_validate_confirmation_of :name, :age
|
56
|
-
end
|
57
|
-
|
5
|
+
|
6
|
+
# Defines a model, create a validation and returns a raw matcher
|
7
|
+
def define_and_validate(options={})
|
8
|
+
@model = define_model :user, :name => :string, :email => :string, :age => :string do
|
9
|
+
validates_confirmation_of :name, :email, options
|
10
|
+
end
|
11
|
+
|
12
|
+
validate_confirmation_of(:name, :email)
|
13
|
+
end
|
14
|
+
|
15
|
+
describe 'messages' do
|
16
|
+
before(:each){ @matcher = define_and_validate }
|
17
|
+
|
18
|
+
it 'should contain a description' do
|
19
|
+
@matcher.description.should == 'require name and email to be confirmed'
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'should set responds_to_confirmation? message' do
|
23
|
+
@matcher = validate_confirmation_of(:age)
|
24
|
+
@matcher.matches?(@model)
|
25
|
+
@matcher.failure_message.should == 'Expected User instance responds to age_confirmation'
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'should set confirms? message' do
|
29
|
+
@model.instance_eval{ def age_confirmation=(*args); end }
|
30
|
+
@matcher = validate_confirmation_of(:age)
|
31
|
+
@matcher.matches?(@model)
|
32
|
+
@matcher.failure_message.should == 'Expected User to be valid only when age is confirmed'
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
|
37
|
+
describe 'matchers' do
|
38
|
+
|
39
|
+
describe 'without options' do
|
40
|
+
before(:each){ define_and_validate }
|
41
|
+
|
42
|
+
it { should validate_confirmation_of(:name) }
|
43
|
+
it { should validate_confirmation_of(:name, :email) }
|
44
|
+
it { should_not validate_confirmation_of(:name, :age) }
|
45
|
+
end
|
46
|
+
|
47
|
+
create_message_specs(self)
|
48
|
+
end
|
49
|
+
|
50
|
+
describe 'macros' do
|
51
|
+
before(:each){ define_and_validate }
|
52
|
+
|
53
|
+
should_validate_confirmation_of :name
|
54
|
+
should_validate_confirmation_of :name, :email
|
55
|
+
should_not_validate_confirmation_of :name, :age
|
56
|
+
end
|
57
|
+
|
58
58
|
end
|
@@ -1,88 +1,88 @@
|
|
1
1
|
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
2
|
|
3
|
-
describe 'validate_exclusion_of' do
|
3
|
+
describe 'validate_exclusion_of' do
|
4
4
|
include ModelBuilder
|
5
|
-
|
6
|
-
# Defines a model, create a validation and returns a raw matcher
|
7
|
-
def define_and_validate(options={})
|
8
|
-
@model = define_model :product, :title => :string, :size => :string do
|
9
|
-
validates_exclusion_of :title, :size, options
|
10
|
-
end
|
11
|
-
|
12
|
-
validate_exclusion_of(:title, :size)
|
13
|
-
end
|
14
|
-
|
15
|
-
describe 'messages' do
|
16
|
-
|
17
|
-
it 'should contain a description' do
|
18
|
-
@matcher = define_and_validate(:in => 2..10)
|
19
|
-
@matcher.in(2..10)
|
20
|
-
@matcher.description.should == 'ensure exclusion of title and size in 2..10'
|
21
|
-
|
22
|
-
@matcher = validate_exclusion_of(:title, :size).in('X', 'Y', 'Z')
|
5
|
+
|
6
|
+
# Defines a model, create a validation and returns a raw matcher
|
7
|
+
def define_and_validate(options={})
|
8
|
+
@model = define_model :product, :title => :string, :size => :string do
|
9
|
+
validates_exclusion_of :title, :size, options
|
10
|
+
end
|
11
|
+
|
12
|
+
validate_exclusion_of(:title, :size)
|
13
|
+
end
|
14
|
+
|
15
|
+
describe 'messages' do
|
16
|
+
|
17
|
+
it 'should contain a description' do
|
18
|
+
@matcher = define_and_validate(:in => 2..10)
|
19
|
+
@matcher.in(2..10)
|
20
|
+
@matcher.description.should == 'ensure exclusion of title and size in 2..10'
|
21
|
+
|
22
|
+
@matcher = validate_exclusion_of(:title, :size).in('X', 'Y', 'Z')
|
23
23
|
@matcher.description.should == 'ensure exclusion of title and size in "X", "Y", and "Z"'
|
24
24
|
end
|
25
|
-
|
26
|
-
it 'should set is_invalid? message' do
|
27
|
-
@matcher = define_and_validate(:in => 2..10)
|
25
|
+
|
26
|
+
it 'should set is_invalid? message' do
|
27
|
+
@matcher = define_and_validate(:in => 2..10)
|
28
28
|
@matcher.in(1..10).matches?(@model)
|
29
|
-
@matcher.failure_message.should == 'Expected Product to be invalid when title is set to 1'
|
30
|
-
end
|
31
|
-
|
32
|
-
it 'should set is_valid? message' do
|
33
|
-
@matcher = define_and_validate(:in => 2..10)
|
29
|
+
@matcher.failure_message.should == 'Expected Product to be invalid when title is set to 1'
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'should set is_valid? message' do
|
33
|
+
@matcher = define_and_validate(:in => 2..10)
|
34
34
|
@matcher.in(3..10).matches?(@model)
|
35
|
-
@matcher.failure_message.should == 'Expected Product to be valid when title is set to 2'
|
36
|
-
end
|
35
|
+
@matcher.failure_message.should == 'Expected Product to be valid when title is set to 2'
|
36
|
+
end
|
37
37
|
|
38
|
-
it 'should set allow_nil? message' do
|
39
|
-
@matcher = define_and_validate(:in => [nil])
|
38
|
+
it 'should set allow_nil? message' do
|
39
|
+
@matcher = define_and_validate(:in => [nil])
|
40
40
|
@matcher.allow_nil.matches?(@model)
|
41
|
-
@matcher.failure_message.should == 'Expected Product to allow nil values for title'
|
42
|
-
end
|
43
|
-
|
44
|
-
it 'should set allow_blank? message' do
|
45
|
-
@matcher = define_and_validate(:in => [''])
|
41
|
+
@matcher.failure_message.should == 'Expected Product to allow nil values for title'
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'should set allow_blank? message' do
|
45
|
+
@matcher = define_and_validate(:in => [''])
|
46
46
|
@matcher.allow_blank.matches?(@model)
|
47
|
-
@matcher.failure_message.should == 'Expected Product to allow blank values for title'
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
describe 'matchers' do
|
52
|
-
it { should define_and_validate(:in => ['X', 'Y', 'Z']).in('X', 'Y', 'Z') }
|
53
|
-
it { should_not define_and_validate(:in => ['X', 'Y', 'Z']).in('A') }
|
54
|
-
|
55
|
-
it { should define_and_validate(:in => 2..3).in(2..3) }
|
56
|
-
it { should define_and_validate(:in => 2..20).in(2..20) }
|
57
|
-
it { should_not define_and_validate(:in => 2..20).in(1..20) }
|
58
|
-
it { should_not define_and_validate(:in => 2..20).in(3..20) }
|
59
|
-
it { should_not define_and_validate(:in => 2..20).in(2..19) }
|
60
|
-
it { should_not define_and_validate(:in => 2..20).in(2..21) }
|
61
|
-
|
62
|
-
it { should define_and_validate(:in => ['X', 'Y', 'Z'], :message => 'valid').in('X', 'Y', 'Z').message('valid') }
|
63
|
-
|
64
|
-
create_optional_boolean_specs(:allow_nil, self, :in => [nil])
|
65
|
-
create_optional_boolean_specs(:allow_blank, self, :in => [''])
|
66
|
-
end
|
67
|
-
|
68
|
-
describe 'macros' do
|
69
|
-
describe 'with array' do
|
70
|
-
before(:each){ define_and_validate(:in => ['X', 'Y', 'Z']) }
|
71
|
-
|
72
|
-
should_validate_exclusion_of :title, :in => ['X']
|
73
|
-
should_validate_exclusion_of :title, :size, :in => ['X']
|
74
|
-
should_not_validate_exclusion_of :title, :size, :in => ['A']
|
75
|
-
end
|
76
|
-
|
77
|
-
describe 'with range' do
|
78
|
-
before(:each){ define_and_validate(:in => 2..20) }
|
79
|
-
|
80
|
-
should_validate_exclusion_of :title, :in => 2..20
|
81
|
-
should_not_validate_exclusion_of :title, :in => 1..20
|
82
|
-
should_not_validate_exclusion_of :title, :in => 3..20
|
83
|
-
should_not_validate_exclusion_of :title, :in => 2..19
|
84
|
-
should_not_validate_exclusion_of :title, :in => 2..21
|
85
|
-
end
|
47
|
+
@matcher.failure_message.should == 'Expected Product to allow blank values for title'
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
describe 'matchers' do
|
52
|
+
it { should define_and_validate(:in => ['X', 'Y', 'Z']).in('X', 'Y', 'Z') }
|
53
|
+
it { should_not define_and_validate(:in => ['X', 'Y', 'Z']).in('A') }
|
54
|
+
|
55
|
+
it { should define_and_validate(:in => 2..3).in(2..3) }
|
56
|
+
it { should define_and_validate(:in => 2..20).in(2..20) }
|
57
|
+
it { should_not define_and_validate(:in => 2..20).in(1..20) }
|
58
|
+
it { should_not define_and_validate(:in => 2..20).in(3..20) }
|
59
|
+
it { should_not define_and_validate(:in => 2..20).in(2..19) }
|
60
|
+
it { should_not define_and_validate(:in => 2..20).in(2..21) }
|
61
|
+
|
62
|
+
it { should define_and_validate(:in => ['X', 'Y', 'Z'], :message => 'valid').in('X', 'Y', 'Z').message('valid') }
|
63
|
+
|
64
|
+
create_optional_boolean_specs(:allow_nil, self, :in => [nil])
|
65
|
+
create_optional_boolean_specs(:allow_blank, self, :in => [''])
|
66
|
+
end
|
67
|
+
|
68
|
+
describe 'macros' do
|
69
|
+
describe 'with array' do
|
70
|
+
before(:each){ define_and_validate(:in => ['X', 'Y', 'Z']) }
|
71
|
+
|
72
|
+
should_validate_exclusion_of :title, :in => ['X']
|
73
|
+
should_validate_exclusion_of :title, :size, :in => ['X']
|
74
|
+
should_not_validate_exclusion_of :title, :size, :in => ['A']
|
75
|
+
end
|
76
|
+
|
77
|
+
describe 'with range' do
|
78
|
+
before(:each){ define_and_validate(:in => 2..20) }
|
79
|
+
|
80
|
+
should_validate_exclusion_of :title, :in => 2..20
|
81
|
+
should_not_validate_exclusion_of :title, :in => 1..20
|
82
|
+
should_not_validate_exclusion_of :title, :in => 3..20
|
83
|
+
should_not_validate_exclusion_of :title, :in => 2..19
|
84
|
+
should_not_validate_exclusion_of :title, :in => 2..21
|
85
|
+
end
|
86
86
|
end
|
87
87
|
end
|
88
|
-
|
88
|
+
|
@@ -1,84 +1,84 @@
|
|
1
1
|
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
2
|
|
3
|
-
describe 'validate_inclusion_of' do
|
3
|
+
describe 'validate_inclusion_of' do
|
4
4
|
include ModelBuilder
|
5
|
-
|
6
|
-
# Defines a model, create a validation and returns a raw matcher
|
7
|
-
def define_and_validate(options={})
|
8
|
-
@model = define_model :product, :title => :string, :size => :string do
|
9
|
-
validates_inclusion_of :title, :size, options
|
10
|
-
end
|
11
|
-
|
12
|
-
validate_inclusion_of(:title, :size)
|
13
|
-
end
|
14
|
-
|
15
|
-
describe 'messages' do
|
16
|
-
before(:each){ @matcher = define_and_validate(:in => 2..10) }
|
17
|
-
|
18
|
-
it 'should contain a description' do
|
19
|
-
@matcher.in(2..10)
|
20
|
-
@matcher.description.should == 'ensure inclusion of title and size in 2..10'
|
21
|
-
|
22
|
-
@matcher = validate_inclusion_of(:title, :size).in('X', 'Y', 'Z')
|
5
|
+
|
6
|
+
# Defines a model, create a validation and returns a raw matcher
|
7
|
+
def define_and_validate(options={})
|
8
|
+
@model = define_model :product, :title => :string, :size => :string do
|
9
|
+
validates_inclusion_of :title, :size, options
|
10
|
+
end
|
11
|
+
|
12
|
+
validate_inclusion_of(:title, :size)
|
13
|
+
end
|
14
|
+
|
15
|
+
describe 'messages' do
|
16
|
+
before(:each){ @matcher = define_and_validate(:in => 2..10) }
|
17
|
+
|
18
|
+
it 'should contain a description' do
|
19
|
+
@matcher.in(2..10)
|
20
|
+
@matcher.description.should == 'ensure inclusion of title and size in 2..10'
|
21
|
+
|
22
|
+
@matcher = validate_inclusion_of(:title, :size).in('X', 'Y', 'Z')
|
23
23
|
@matcher.description.should == 'ensure inclusion of title and size in "X", "Y", and "Z"'
|
24
24
|
end
|
25
|
-
|
26
|
-
it 'should set is_valid? message' do
|
25
|
+
|
26
|
+
it 'should set is_valid? message' do
|
27
27
|
@matcher.in(1..10).matches?(@model)
|
28
|
-
@matcher.failure_message.should == 'Expected Product to be valid when title is set to 1'
|
29
|
-
end
|
30
|
-
|
31
|
-
it 'should set is_invalid? message' do
|
28
|
+
@matcher.failure_message.should == 'Expected Product to be valid when title is set to 1'
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'should set is_invalid? message' do
|
32
32
|
@matcher.in(3..10).matches?(@model)
|
33
|
-
@matcher.failure_message.should == 'Expected Product to be invalid when title is set to 2'
|
34
|
-
end
|
33
|
+
@matcher.failure_message.should == 'Expected Product to be invalid when title is set to 2'
|
34
|
+
end
|
35
35
|
|
36
|
-
it 'should set allow_nil? message' do
|
36
|
+
it 'should set allow_nil? message' do
|
37
37
|
@matcher.allow_nil.matches?(@model)
|
38
|
-
@matcher.failure_message.should == 'Expected Product to allow nil values for title'
|
39
|
-
end
|
40
|
-
|
41
|
-
it 'should set allow_blank? message' do
|
38
|
+
@matcher.failure_message.should == 'Expected Product to allow nil values for title'
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'should set allow_blank? message' do
|
42
42
|
@matcher.allow_blank.matches?(@model)
|
43
|
-
@matcher.failure_message.should == 'Expected Product to allow blank values for title'
|
44
|
-
end
|
45
|
-
end
|
46
|
-
|
47
|
-
describe 'matchers' do
|
48
|
-
it { should define_and_validate(:in => ['X', 'Y', 'Z']).in('X', 'Y', 'Z') }
|
49
|
-
it { should_not define_and_validate(:in => ['X', 'Y', 'Z']).in('A') }
|
50
|
-
|
51
|
-
it { should define_and_validate(:in => 2..3).in(2..3) }
|
52
|
-
it { should define_and_validate(:in => 2..20).in(2..20) }
|
53
|
-
it { should_not define_and_validate(:in => 2..20).in(1..20) }
|
54
|
-
it { should_not define_and_validate(:in => 2..20).in(3..20) }
|
55
|
-
it { should_not define_and_validate(:in => 2..20).in(2..19) }
|
56
|
-
it { should_not define_and_validate(:in => 2..20).in(2..21) }
|
57
|
-
|
58
|
-
it { should define_and_validate(:in => ['X', 'Y', 'Z'], :message => 'valid').in('X', 'Y', 'Z').message('valid') }
|
59
|
-
|
60
|
-
create_optional_boolean_specs(:allow_nil, self, :in => ['X'])
|
61
|
-
create_optional_boolean_specs(:allow_blank, self, :in => ['X'])
|
62
|
-
end
|
63
|
-
|
64
|
-
describe 'macros' do
|
65
|
-
describe 'with array' do
|
66
|
-
before(:each){ define_and_validate(:in => ['X', 'Y', 'Z']) }
|
67
|
-
|
68
|
-
should_validate_inclusion_of :title, :in => ['X']
|
69
|
-
should_validate_inclusion_of :title, :size, :in => ['X']
|
70
|
-
should_not_validate_inclusion_of :title, :size, :in => ['A']
|
71
|
-
end
|
72
|
-
|
73
|
-
describe 'with range' do
|
74
|
-
before(:each){ define_and_validate(:in => 2..20) }
|
75
|
-
|
76
|
-
should_validate_inclusion_of :title, :in => 2..20
|
77
|
-
should_not_validate_inclusion_of :title, :in => 1..20
|
78
|
-
should_not_validate_inclusion_of :title, :in => 3..20
|
79
|
-
should_not_validate_inclusion_of :title, :in => 2..19
|
80
|
-
should_not_validate_inclusion_of :title, :in => 2..21
|
81
|
-
end
|
43
|
+
@matcher.failure_message.should == 'Expected Product to allow blank values for title'
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
describe 'matchers' do
|
48
|
+
it { should define_and_validate(:in => ['X', 'Y', 'Z']).in('X', 'Y', 'Z') }
|
49
|
+
it { should_not define_and_validate(:in => ['X', 'Y', 'Z']).in('A') }
|
50
|
+
|
51
|
+
it { should define_and_validate(:in => 2..3).in(2..3) }
|
52
|
+
it { should define_and_validate(:in => 2..20).in(2..20) }
|
53
|
+
it { should_not define_and_validate(:in => 2..20).in(1..20) }
|
54
|
+
it { should_not define_and_validate(:in => 2..20).in(3..20) }
|
55
|
+
it { should_not define_and_validate(:in => 2..20).in(2..19) }
|
56
|
+
it { should_not define_and_validate(:in => 2..20).in(2..21) }
|
57
|
+
|
58
|
+
it { should define_and_validate(:in => ['X', 'Y', 'Z'], :message => 'valid').in('X', 'Y', 'Z').message('valid') }
|
59
|
+
|
60
|
+
create_optional_boolean_specs(:allow_nil, self, :in => ['X'])
|
61
|
+
create_optional_boolean_specs(:allow_blank, self, :in => ['X'])
|
62
|
+
end
|
63
|
+
|
64
|
+
describe 'macros' do
|
65
|
+
describe 'with array' do
|
66
|
+
before(:each){ define_and_validate(:in => ['X', 'Y', 'Z']) }
|
67
|
+
|
68
|
+
should_validate_inclusion_of :title, :in => ['X']
|
69
|
+
should_validate_inclusion_of :title, :size, :in => ['X']
|
70
|
+
should_not_validate_inclusion_of :title, :size, :in => ['A']
|
71
|
+
end
|
72
|
+
|
73
|
+
describe 'with range' do
|
74
|
+
before(:each){ define_and_validate(:in => 2..20) }
|
75
|
+
|
76
|
+
should_validate_inclusion_of :title, :in => 2..20
|
77
|
+
should_not_validate_inclusion_of :title, :in => 1..20
|
78
|
+
should_not_validate_inclusion_of :title, :in => 3..20
|
79
|
+
should_not_validate_inclusion_of :title, :in => 2..19
|
80
|
+
should_not_validate_inclusion_of :title, :in => 2..21
|
81
|
+
end
|
82
82
|
end
|
83
83
|
end
|
84
|
-
|
84
|
+
|
@@ -1,22 +1,22 @@
|
|
1
1
|
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
2
|
|
3
|
-
describe 'validate_numericality_of' do
|
3
|
+
describe 'validate_numericality_of' do
|
4
4
|
include ModelBuilder
|
5
|
-
|
6
|
-
# Defines a model, create a validation and returns a raw matcher
|
7
|
-
def define_and_validate(options={})
|
8
|
-
@model = define_model :product, :price => :integer, :size => :integer do
|
9
|
-
validates_numericality_of :price, options
|
10
|
-
end
|
11
|
-
|
12
|
-
validate_numericality_of(:price)
|
13
|
-
end
|
14
|
-
|
15
|
-
describe 'messages' do
|
16
|
-
before(:each){ @matcher = define_and_validate }
|
17
|
-
|
18
|
-
# To tests descriptions, we don't need to use mocks neither stubs.
|
19
|
-
# We just need to define a matcher.
|
5
|
+
|
6
|
+
# Defines a model, create a validation and returns a raw matcher
|
7
|
+
def define_and_validate(options={})
|
8
|
+
@model = define_model :product, :price => :integer, :size => :integer do
|
9
|
+
validates_numericality_of :price, options
|
10
|
+
end
|
11
|
+
|
12
|
+
validate_numericality_of(:price)
|
13
|
+
end
|
14
|
+
|
15
|
+
describe 'messages' do
|
16
|
+
before(:each){ @matcher = define_and_validate }
|
17
|
+
|
18
|
+
# To tests descriptions, we don't need to use mocks neither stubs.
|
19
|
+
# We just need to define a matcher.
|
20
20
|
#
|
21
21
|
it 'should contain a description' do
|
22
22
|
matcher = validate_numericality_of(:age)
|
@@ -50,23 +50,23 @@ describe 'validate_numericality_of' do
|
|
50
50
|
matcher.description.should == 'ensure numericality of age is less than 10'
|
51
51
|
|
52
52
|
matcher = validate_numericality_of(:age, :greater_than => 10)
|
53
|
-
matcher.description.should == 'ensure numericality of age is greater than 10'
|
54
|
-
|
53
|
+
matcher.description.should == 'ensure numericality of age is greater than 10'
|
54
|
+
|
55
55
|
matcher = validate_numericality_of(:age, :greater_than => 10, :less_than => 20)
|
56
56
|
matcher.description.should == 'ensure numericality of age is greater than 10 and is less than 20'
|
57
57
|
end
|
58
|
-
|
58
|
+
|
59
59
|
it 'should set only_numeric_values? message' do
|
60
60
|
@matcher.should_receive(:only_numeric_values?).and_return(false)
|
61
61
|
@matcher.matches?(@model)
|
62
|
-
@matcher.failure_message.should == 'Expected Product to allow only numeric values for price'
|
62
|
+
@matcher.failure_message.should == 'Expected Product to allow only numeric values for price'
|
63
63
|
@matcher.negative_failure_message.should == 'Did not expect Product to allow only numeric values for price'
|
64
64
|
end
|
65
65
|
|
66
66
|
it 'should set only_integer_values? message' do
|
67
67
|
@matcher.should_receive(:only_integer?).and_return([false, { :not => '' }])
|
68
68
|
@matcher.matches?(@model)
|
69
|
-
@matcher.failure_message.should == 'Expected Product to allow only integer values for price'
|
69
|
+
@matcher.failure_message.should == 'Expected Product to allow only integer values for price'
|
70
70
|
end
|
71
71
|
|
72
72
|
it 'should set only_odd_values? message' do
|
@@ -97,84 +97,84 @@ describe 'validate_numericality_of' do
|
|
97
97
|
@matcher.should_receive(:more_than_maximum?).and_return([false, { :count => 10 }])
|
98
98
|
@matcher.matches?(@model)
|
99
99
|
@matcher.failure_message.should == 'Expected Product to be invalid when price is greater than 10'
|
100
|
-
end
|
101
|
-
end
|
102
|
-
|
103
|
-
describe 'matcher' do
|
104
|
-
# Wrap specs without options. Usually a couple specs.
|
105
|
-
describe 'without options' do
|
106
|
-
before(:each){ define_and_validate }
|
107
|
-
|
108
|
-
it { should validate_numericality_of(:price) }
|
109
|
-
it { should_not validate_numericality_of(:size) }
|
110
|
-
end
|
111
|
-
|
112
|
-
# Wrap each option inside a describe group.
|
113
|
-
describe 'with equal_to option' do
|
114
|
-
it { should define_and_validate(:equal_to => 100).equal_to(100) }
|
115
|
-
it { should_not define_and_validate(:equal_to => 100).equal_to(99) }
|
116
|
-
it { should_not define_and_validate(:equal_to => 100).equal_to(101) }
|
117
|
-
end
|
118
|
-
|
119
|
-
describe 'with less_than option' do
|
120
|
-
it { should define_and_validate(:less_than => 100).less_than(100) }
|
121
|
-
it { should_not define_and_validate(:less_than => 100).less_than(99) }
|
122
|
-
it { should_not define_and_validate(:less_than => 100).less_than(101) }
|
123
|
-
end
|
124
|
-
|
125
|
-
describe 'with greater_than option' do
|
126
|
-
it { should define_and_validate(:greater_than => 100).greater_than(100) }
|
127
|
-
it { should_not define_and_validate(:greater_than => 100).greater_than(99) }
|
128
|
-
it { should_not define_and_validate(:greater_than => 100).greater_than(101) }
|
129
|
-
end
|
130
|
-
|
131
|
-
describe 'with less_than_or_equal_to option' do
|
132
|
-
it { should define_and_validate(:less_than_or_equal_to => 100).less_than_or_equal_to(100) }
|
133
|
-
it { should_not define_and_validate(:less_than_or_equal_to => 100).less_than_or_equal_to(99) }
|
134
|
-
it { should_not define_and_validate(:less_than_or_equal_to => 100).less_than_or_equal_to(101) }
|
135
|
-
end
|
136
|
-
|
137
|
-
describe 'with greater_than_or_equal_to option' do
|
138
|
-
it { should define_and_validate(:greater_than_or_equal_to => 100).greater_than_or_equal_to(100) }
|
139
|
-
it { should_not define_and_validate(:greater_than_or_equal_to => 100).greater_than_or_equal_to(99) }
|
140
|
-
it { should_not define_and_validate(:greater_than_or_equal_to => 100).greater_than_or_equal_to(101) }
|
141
|
-
end
|
142
|
-
|
143
|
-
describe "with even option" do
|
144
|
-
it { should define_and_validate(:even => true).even }
|
145
|
-
it { should_not define_and_validate.even(true) }
|
146
|
-
end
|
147
|
-
|
148
|
-
describe "with odd option" do
|
149
|
-
it { should define_and_validate(:odd => true).odd }
|
150
|
-
it { should_not define_and_validate.odd(true) }
|
151
|
-
end
|
152
|
-
|
153
|
-
describe "with several options" do
|
154
|
-
it { should define_and_validate(:less_than => 100, :greater_than => 10).less_than(100).greater_than(10) }
|
155
|
-
it { should define_and_validate(:less_than => 100, :message => 'not valid').less_than(100).message('not valid') }
|
156
|
-
it { should define_and_validate(:less_than_or_equal_to => 100, :greater_than => 1).less_than_or_equal_to(100).greater_than(1) }
|
157
|
-
end
|
158
|
-
|
159
|
-
# A macro to spec messages
|
160
|
-
create_message_specs(self)
|
161
|
-
|
162
|
-
# Those are macros to test optionals which accept only boolean values
|
163
|
-
create_optional_boolean_specs(:allow_nil, self)
|
164
|
-
create_optional_boolean_specs(:allow_blank, self)
|
165
|
-
create_optional_boolean_specs(:only_integer, self)
|
166
|
-
end
|
167
|
-
|
168
|
-
# In macros we include just a few tests to assure that everything works properly
|
169
|
-
describe 'macros' do
|
170
|
-
before(:each) { define_and_validate(:less_than => 100000, :greater_than => 9999) }
|
171
|
-
|
172
|
-
should_validate_numericality_of :price
|
173
|
-
should_validate_numericality_of :price, :less_than => 100000
|
174
|
-
should_validate_numericality_of :price, :greater_than => 9999
|
175
|
-
should_validate_numericality_of :price, :less_than => 100000, :greater_than => 999
|
176
|
-
should_not_validate_numericality_of :size
|
177
|
-
should_not_validate_numericality_of :price, :less_than => 55555
|
178
|
-
should_not_validate_numericality_of :price, :greater_than => 55555
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
describe 'matcher' do
|
104
|
+
# Wrap specs without options. Usually a couple specs.
|
105
|
+
describe 'without options' do
|
106
|
+
before(:each){ define_and_validate }
|
107
|
+
|
108
|
+
it { should validate_numericality_of(:price) }
|
109
|
+
it { should_not validate_numericality_of(:size) }
|
110
|
+
end
|
111
|
+
|
112
|
+
# Wrap each option inside a describe group.
|
113
|
+
describe 'with equal_to option' do
|
114
|
+
it { should define_and_validate(:equal_to => 100).equal_to(100) }
|
115
|
+
it { should_not define_and_validate(:equal_to => 100).equal_to(99) }
|
116
|
+
it { should_not define_and_validate(:equal_to => 100).equal_to(101) }
|
117
|
+
end
|
118
|
+
|
119
|
+
describe 'with less_than option' do
|
120
|
+
it { should define_and_validate(:less_than => 100).less_than(100) }
|
121
|
+
it { should_not define_and_validate(:less_than => 100).less_than(99) }
|
122
|
+
it { should_not define_and_validate(:less_than => 100).less_than(101) }
|
123
|
+
end
|
124
|
+
|
125
|
+
describe 'with greater_than option' do
|
126
|
+
it { should define_and_validate(:greater_than => 100).greater_than(100) }
|
127
|
+
it { should_not define_and_validate(:greater_than => 100).greater_than(99) }
|
128
|
+
it { should_not define_and_validate(:greater_than => 100).greater_than(101) }
|
129
|
+
end
|
130
|
+
|
131
|
+
describe 'with less_than_or_equal_to option' do
|
132
|
+
it { should define_and_validate(:less_than_or_equal_to => 100).less_than_or_equal_to(100) }
|
133
|
+
it { should_not define_and_validate(:less_than_or_equal_to => 100).less_than_or_equal_to(99) }
|
134
|
+
it { should_not define_and_validate(:less_than_or_equal_to => 100).less_than_or_equal_to(101) }
|
135
|
+
end
|
136
|
+
|
137
|
+
describe 'with greater_than_or_equal_to option' do
|
138
|
+
it { should define_and_validate(:greater_than_or_equal_to => 100).greater_than_or_equal_to(100) }
|
139
|
+
it { should_not define_and_validate(:greater_than_or_equal_to => 100).greater_than_or_equal_to(99) }
|
140
|
+
it { should_not define_and_validate(:greater_than_or_equal_to => 100).greater_than_or_equal_to(101) }
|
141
|
+
end
|
142
|
+
|
143
|
+
describe "with even option" do
|
144
|
+
it { should define_and_validate(:even => true).even }
|
145
|
+
it { should_not define_and_validate.even(true) }
|
146
|
+
end
|
147
|
+
|
148
|
+
describe "with odd option" do
|
149
|
+
it { should define_and_validate(:odd => true).odd }
|
150
|
+
it { should_not define_and_validate.odd(true) }
|
151
|
+
end
|
152
|
+
|
153
|
+
describe "with several options" do
|
154
|
+
it { should define_and_validate(:less_than => 100, :greater_than => 10).less_than(100).greater_than(10) }
|
155
|
+
it { should define_and_validate(:less_than => 100, :message => 'not valid').less_than(100).message('not valid') }
|
156
|
+
it { should define_and_validate(:less_than_or_equal_to => 100, :greater_than => 1).less_than_or_equal_to(100).greater_than(1) }
|
157
|
+
end
|
158
|
+
|
159
|
+
# A macro to spec messages
|
160
|
+
create_message_specs(self)
|
161
|
+
|
162
|
+
# Those are macros to test optionals which accept only boolean values
|
163
|
+
create_optional_boolean_specs(:allow_nil, self)
|
164
|
+
create_optional_boolean_specs(:allow_blank, self)
|
165
|
+
create_optional_boolean_specs(:only_integer, self)
|
166
|
+
end
|
167
|
+
|
168
|
+
# In macros we include just a few tests to assure that everything works properly
|
169
|
+
describe 'macros' do
|
170
|
+
before(:each) { define_and_validate(:less_than => 100000, :greater_than => 9999) }
|
171
|
+
|
172
|
+
should_validate_numericality_of :price
|
173
|
+
should_validate_numericality_of :price, :less_than => 100000
|
174
|
+
should_validate_numericality_of :price, :greater_than => 9999
|
175
|
+
should_validate_numericality_of :price, :less_than => 100000, :greater_than => 999
|
176
|
+
should_not_validate_numericality_of :size
|
177
|
+
should_not_validate_numericality_of :price, :less_than => 55555
|
178
|
+
should_not_validate_numericality_of :price, :greater_than => 55555
|
179
179
|
end
|
180
180
|
end
|