shoulda-matchers 2.5.0 → 2.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (121) hide show
  1. data/.gitignore +8 -7
  2. data/.travis.yml +4 -0
  3. data/Appraisals +8 -0
  4. data/CONTRIBUTING.md +1 -1
  5. data/Gemfile +1 -1
  6. data/Gemfile.lock +77 -66
  7. data/MIT-LICENSE +1 -1
  8. data/NEWS.md +63 -1
  9. data/README.md +189 -33
  10. data/Rakefile +6 -5
  11. data/features/rails_integration.feature +1 -1
  12. data/features/step_definitions/rails_steps.rb +7 -6
  13. data/gemfiles/3.0.gemfile +2 -2
  14. data/gemfiles/3.0.gemfile.lock +14 -5
  15. data/gemfiles/3.1.gemfile +2 -2
  16. data/gemfiles/3.1.gemfile.lock +14 -5
  17. data/gemfiles/3.2.gemfile +2 -2
  18. data/gemfiles/3.2.gemfile.lock +16 -7
  19. data/gemfiles/4.0.0.gemfile +2 -2
  20. data/gemfiles/4.0.0.gemfile.lock +15 -6
  21. data/gemfiles/4.0.1.gemfile +2 -2
  22. data/gemfiles/4.0.1.gemfile.lock +15 -6
  23. data/gemfiles/4.1.gemfile +19 -0
  24. data/gemfiles/4.1.gemfile.lock +176 -0
  25. data/lib/shoulda/matchers.rb +17 -1
  26. data/lib/shoulda/matchers/action_controller.rb +4 -2
  27. data/lib/shoulda/matchers/action_controller/callback_matcher.rb +100 -0
  28. data/lib/shoulda/matchers/action_controller/redirect_to_matcher.rb +1 -1
  29. data/lib/shoulda/matchers/action_controller/render_template_matcher.rb +4 -4
  30. data/lib/shoulda/matchers/action_controller/rescue_from_matcher.rb +1 -1
  31. data/lib/shoulda/matchers/action_controller/route_matcher.rb +12 -12
  32. data/lib/shoulda/matchers/action_controller/route_params.rb +1 -1
  33. data/lib/shoulda/matchers/action_controller/set_the_flash_matcher.rb +2 -1
  34. data/lib/shoulda/matchers/action_controller/strong_parameters_matcher.rb +167 -0
  35. data/lib/shoulda/matchers/active_model.rb +4 -2
  36. data/lib/shoulda/matchers/active_model/allow_value_matcher.rb +23 -5
  37. data/lib/shoulda/matchers/active_model/disallow_value_matcher.rb +0 -4
  38. data/lib/shoulda/matchers/active_model/ensure_inclusion_of_matcher.rb +66 -14
  39. data/lib/shoulda/matchers/active_model/ensure_length_of_matcher.rb +8 -8
  40. data/lib/shoulda/matchers/active_model/errors.rb +40 -0
  41. data/lib/shoulda/matchers/active_model/helpers.rb +6 -6
  42. data/lib/shoulda/matchers/active_model/numericality_matchers/comparison_matcher.rb +33 -14
  43. data/lib/shoulda/matchers/active_model/numericality_matchers/even_number_matcher.rb +26 -0
  44. data/lib/shoulda/matchers/active_model/numericality_matchers/{odd_even_number_matcher.rb → numeric_type_matcher.rb} +9 -20
  45. data/lib/shoulda/matchers/active_model/numericality_matchers/odd_number_matcher.rb +26 -0
  46. data/lib/shoulda/matchers/active_model/numericality_matchers/only_integer_matcher.rb +5 -21
  47. data/lib/shoulda/matchers/active_model/validate_confirmation_of_matcher.rb +1 -1
  48. data/lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb +71 -22
  49. data/lib/shoulda/matchers/active_model/validate_presence_of_matcher.rb +6 -1
  50. data/lib/shoulda/matchers/active_model/validate_uniqueness_of_matcher.rb +25 -6
  51. data/lib/shoulda/matchers/active_record.rb +1 -0
  52. data/lib/shoulda/matchers/active_record/association_matcher.rb +67 -13
  53. data/lib/shoulda/matchers/active_record/association_matchers/inverse_of_matcher.rb +40 -0
  54. data/lib/shoulda/matchers/active_record/association_matchers/model_reflection.rb +24 -1
  55. data/lib/shoulda/matchers/active_record/association_matchers/model_reflector.rb +1 -1
  56. data/lib/shoulda/matchers/active_record/have_db_column_matcher.rb +1 -1
  57. data/lib/shoulda/matchers/active_record/have_db_index_matcher.rb +1 -1
  58. data/lib/shoulda/matchers/assertion_error.rb +7 -2
  59. data/lib/shoulda/matchers/error.rb +24 -0
  60. data/lib/shoulda/matchers/independent.rb +10 -0
  61. data/lib/shoulda/matchers/independent/delegate_matcher.rb +157 -0
  62. data/lib/shoulda/matchers/independent/delegate_matcher/stubbed_target.rb +34 -0
  63. data/lib/shoulda/matchers/integrations/nunit_test_case_detection.rb +36 -0
  64. data/lib/shoulda/matchers/integrations/rspec.rb +13 -14
  65. data/lib/shoulda/matchers/integrations/test_unit.rb +11 -9
  66. data/lib/shoulda/matchers/version.rb +1 -1
  67. data/lib/shoulda/matchers/warn.rb +7 -0
  68. data/shoulda-matchers.gemspec +2 -1
  69. data/spec/shoulda/matchers/action_controller/callback_matcher_spec.rb +79 -0
  70. data/spec/shoulda/matchers/action_controller/filter_param_matcher_spec.rb +3 -3
  71. data/spec/shoulda/matchers/action_controller/redirect_to_matcher_spec.rb +11 -11
  72. data/spec/shoulda/matchers/action_controller/render_template_matcher_spec.rb +21 -21
  73. data/spec/shoulda/matchers/action_controller/render_with_layout_matcher_spec.rb +10 -10
  74. data/spec/shoulda/matchers/action_controller/rescue_from_matcher_spec.rb +45 -18
  75. data/spec/shoulda/matchers/action_controller/respond_with_matcher_spec.rb +8 -8
  76. data/spec/shoulda/matchers/action_controller/route_matcher_spec.rb +19 -19
  77. data/spec/shoulda/matchers/action_controller/route_params_spec.rb +6 -6
  78. data/spec/shoulda/matchers/action_controller/set_session_matcher_spec.rb +11 -11
  79. data/spec/shoulda/matchers/action_controller/set_the_flash_matcher_spec.rb +44 -44
  80. data/spec/shoulda/matchers/action_controller/strong_parameters_matcher_spec.rb +205 -0
  81. data/spec/shoulda/matchers/active_model/allow_mass_assignment_of_matcher_spec.rb +24 -24
  82. data/spec/shoulda/matchers/active_model/allow_value_matcher_spec.rb +37 -37
  83. data/spec/shoulda/matchers/active_model/disallow_value_matcher_spec.rb +17 -21
  84. data/spec/shoulda/matchers/active_model/ensure_exclusion_of_matcher_spec.rb +24 -24
  85. data/spec/shoulda/matchers/active_model/ensure_inclusion_of_matcher_spec.rb +173 -67
  86. data/spec/shoulda/matchers/active_model/ensure_length_of_matcher_spec.rb +40 -40
  87. data/spec/shoulda/matchers/active_model/exception_message_finder_spec.rb +20 -20
  88. data/spec/shoulda/matchers/active_model/helpers_spec.rb +27 -25
  89. data/spec/shoulda/matchers/active_model/numericality_matchers/comparison_matcher_spec.rb +126 -13
  90. data/spec/shoulda/matchers/active_model/numericality_matchers/even_number_matcher_spec.rb +59 -0
  91. data/spec/shoulda/matchers/active_model/numericality_matchers/odd_number_matcher_spec.rb +59 -0
  92. data/spec/shoulda/matchers/active_model/numericality_matchers/only_integer_matcher_spec.rb +27 -26
  93. data/spec/shoulda/matchers/active_model/validate_absence_of_matcher_spec.rb +15 -15
  94. data/spec/shoulda/matchers/active_model/validate_acceptance_of_matcher_spec.rb +8 -8
  95. data/spec/shoulda/matchers/active_model/validate_confirmation_of_matcher_spec.rb +9 -9
  96. data/spec/shoulda/matchers/active_model/validate_numericality_of_matcher_spec.rb +229 -44
  97. data/spec/shoulda/matchers/active_model/validate_presence_of_matcher_spec.rb +44 -25
  98. data/spec/shoulda/matchers/active_model/validate_uniqueness_of_matcher_spec.rb +110 -62
  99. data/spec/shoulda/matchers/active_model/validation_message_finder_spec.rb +19 -19
  100. data/spec/shoulda/matchers/active_record/accept_nested_attributes_for_matcher_spec.rb +30 -30
  101. data/spec/shoulda/matchers/active_record/association_matcher_spec.rb +378 -192
  102. data/spec/shoulda/matchers/active_record/association_matchers/model_reflection_spec.rb +4 -0
  103. data/spec/shoulda/matchers/active_record/have_db_column_matcher_spec.rb +33 -33
  104. data/spec/shoulda/matchers/active_record/have_db_index_matcher_spec.rb +21 -17
  105. data/spec/shoulda/matchers/active_record/have_readonly_attributes_matcher_spec.rb +8 -8
  106. data/spec/shoulda/matchers/active_record/serialize_matcher_spec.rb +14 -14
  107. data/spec/shoulda/matchers/independent/delegate_matcher/stubbed_target_spec.rb +43 -0
  108. data/spec/shoulda/matchers/independent/delegate_matcher_spec.rb +184 -0
  109. data/spec/spec_helper.rb +4 -0
  110. data/spec/support/activemodel_helpers.rb +2 -2
  111. data/spec/support/capture_helpers.rb +19 -0
  112. data/spec/support/controller_builder.rb +22 -3
  113. data/spec/support/fail_with_message_including_matcher.rb +33 -0
  114. data/spec/support/model_builder.rb +1 -1
  115. data/spec/support/shared_examples/numerical_submatcher.rb +19 -0
  116. data/spec/support/shared_examples/numerical_type_submatcher.rb +17 -0
  117. data/spec/support/test_application.rb +23 -0
  118. metadata +90 -22
  119. checksums.yaml +0 -7
  120. data/spec/shoulda/matchers/active_model/numericality_matchers/odd_even_number_matcher_spec.rb +0 -97
  121. data/spec/support/shared_examples/numerical_submatcher_spec.rb +0 -23
@@ -3,112 +3,112 @@ require 'spec_helper'
3
3
  describe Shoulda::Matchers::ActiveModel::EnsureLengthOfMatcher do
4
4
  context 'an attribute with a non-zero minimum length validation' do
5
5
  it 'accepts ensuring the correct minimum length' do
6
- validating_length(:minimum => 4).
7
- should ensure_length_of(:attr).is_at_least(4)
6
+ expect(validating_length(minimum: 4)).
7
+ to ensure_length_of(:attr).is_at_least(4)
8
8
  end
9
9
 
10
10
  it 'rejects ensuring a lower minimum length with any message' do
11
- validating_length(:minimum => 4).
12
- should_not ensure_length_of(:attr).is_at_least(3).with_short_message(/.*/)
11
+ expect(validating_length(minimum: 4)).
12
+ not_to ensure_length_of(:attr).is_at_least(3).with_short_message(/.*/)
13
13
  end
14
14
 
15
15
  it 'rejects ensuring a higher minimum length with any message' do
16
- validating_length(:minimum => 4).
17
- should_not ensure_length_of(:attr).is_at_least(5).with_short_message(/.*/)
16
+ expect(validating_length(minimum: 4)).
17
+ not_to ensure_length_of(:attr).is_at_least(5).with_short_message(/.*/)
18
18
  end
19
19
 
20
20
  it 'does not override the default message with a blank' do
21
- validating_length(:minimum => 4).
22
- should ensure_length_of(:attr).is_at_least(4).with_short_message(nil)
21
+ expect(validating_length(minimum: 4)).
22
+ to ensure_length_of(:attr).is_at_least(4).with_short_message(nil)
23
23
  end
24
24
  end
25
25
 
26
26
  context 'an attribute with a minimum length validation of 0' do
27
27
  it 'accepts ensuring the correct minimum length' do
28
- validating_length(:minimum => 0).
29
- should ensure_length_of(:attr).is_at_least(0)
28
+ expect(validating_length(minimum: 0)).
29
+ to ensure_length_of(:attr).is_at_least(0)
30
30
  end
31
31
  end
32
32
 
33
33
  context 'an attribute with a maximum length' do
34
34
  it 'accepts ensuring the correct maximum length' do
35
- validating_length(:maximum => 4).
36
- should ensure_length_of(:attr).is_at_most(4)
35
+ expect(validating_length(maximum: 4)).
36
+ to ensure_length_of(:attr).is_at_most(4)
37
37
  end
38
38
 
39
39
  it 'rejects ensuring a lower maximum length with any message' do
40
- validating_length(:maximum => 4).
41
- should_not ensure_length_of(:attr).is_at_most(3).with_long_message(/.*/)
40
+ expect(validating_length(maximum: 4)).
41
+ not_to ensure_length_of(:attr).is_at_most(3).with_long_message(/.*/)
42
42
  end
43
43
 
44
44
  it 'rejects ensuring a higher maximum length with any message' do
45
- validating_length(:maximum => 4).
46
- should_not ensure_length_of(:attr).is_at_most(5).with_long_message(/.*/)
45
+ expect(validating_length(maximum: 4)).
46
+ not_to ensure_length_of(:attr).is_at_most(5).with_long_message(/.*/)
47
47
  end
48
48
 
49
49
  it 'does not override the default message with a blank' do
50
- validating_length(:maximum => 4).
51
- should ensure_length_of(:attr).is_at_most(4).with_long_message(nil)
50
+ expect(validating_length(maximum: 4)).
51
+ to ensure_length_of(:attr).is_at_most(4).with_long_message(nil)
52
52
  end
53
53
  end
54
54
 
55
55
  context 'an attribute with a required exact length' do
56
56
  it 'accepts ensuring the correct length' do
57
- validating_length(:is => 4).should ensure_length_of(:attr).is_equal_to(4)
57
+ expect(validating_length(is: 4)).to ensure_length_of(:attr).is_equal_to(4)
58
58
  end
59
59
 
60
60
  it 'rejects ensuring a lower maximum length with any message' do
61
- validating_length(:is => 4).
62
- should_not ensure_length_of(:attr).is_equal_to(3).with_message(/.*/)
61
+ expect(validating_length(is: 4)).
62
+ not_to ensure_length_of(:attr).is_equal_to(3).with_message(/.*/)
63
63
  end
64
64
 
65
65
  it 'rejects ensuring a higher maximum length with any message' do
66
- validating_length(:is => 4).
67
- should_not ensure_length_of(:attr).is_equal_to(5).with_message(/.*/)
66
+ expect(validating_length(is: 4)).
67
+ not_to ensure_length_of(:attr).is_equal_to(5).with_message(/.*/)
68
68
  end
69
69
 
70
70
  it 'does not override the default message with a blank' do
71
- validating_length(:is => 4).
72
- should ensure_length_of(:attr).is_equal_to(4).with_message(nil)
71
+ expect(validating_length(is: 4)).
72
+ to ensure_length_of(:attr).is_equal_to(4).with_message(nil)
73
73
  end
74
74
  end
75
75
 
76
76
  context 'an attribute with a required exact length and another validation' do
77
77
  it 'accepts ensuring the correct length' do
78
- model = define_model(:example, :attr => :string) do
79
- validates_length_of :attr, :is => 4
78
+ model = define_model(:example, attr: :string) do
79
+ validates_length_of :attr, is: 4
80
80
  validates_numericality_of :attr
81
81
  end.new
82
82
 
83
- model.should ensure_length_of(:attr).is_equal_to(4)
83
+ expect(model).to ensure_length_of(:attr).is_equal_to(4)
84
84
  end
85
85
  end
86
86
 
87
87
  context 'an attribute with a custom minimum length validation' do
88
88
  it 'accepts ensuring the correct minimum length' do
89
- validating_length(:minimum => 4, :too_short => 'foobar').
90
- should ensure_length_of(:attr).is_at_least(4).with_short_message(/foo/)
89
+ expect(validating_length(minimum: 4, too_short: 'foobar')).
90
+ to ensure_length_of(:attr).is_at_least(4).with_short_message(/foo/)
91
91
  end
92
92
  end
93
93
 
94
94
  context 'an attribute with a custom maximum length validation' do
95
95
  it 'accepts ensuring the correct minimum length' do
96
- validating_length(:maximum => 4, :too_long => 'foobar').
97
- should ensure_length_of(:attr).is_at_most(4).with_long_message(/foo/)
96
+ expect(validating_length(maximum: 4, too_long: 'foobar')).
97
+ to ensure_length_of(:attr).is_at_most(4).with_long_message(/foo/)
98
98
  end
99
99
  end
100
100
 
101
101
  context 'an attribute with a custom equal validation' do
102
102
  it 'accepts ensuring the correct exact length' do
103
- validating_length(:is => 4, :message => 'foobar').
104
- should ensure_length_of(:attr).is_equal_to(4).with_message(/foo/)
103
+ expect(validating_length(is: 4, message: 'foobar')).
104
+ to ensure_length_of(:attr).is_equal_to(4).with_message(/foo/)
105
105
  end
106
106
  end
107
107
 
108
108
  context 'an attribute without a length validation' do
109
109
  it 'rejects ensuring a minimum length' do
110
- define_model(:example, :attr => :string).new.
111
- should_not ensure_length_of(:attr).is_at_least(1)
110
+ expect(define_model(:example, attr: :string).new).
111
+ not_to ensure_length_of(:attr).is_at_least(1)
112
112
  end
113
113
  end
114
114
 
@@ -124,7 +124,7 @@ describe Shoulda::Matchers::ActiveModel::EnsureLengthOfMatcher do
124
124
 
125
125
  it "does not raise an exception" do
126
126
  expect {
127
- validating_length(:maximum => 4).should ensure_length_of(:attr).is_at_most(4)
127
+ expect(validating_length(maximum: 4)).to ensure_length_of(:attr).is_at_most(4)
128
128
  }.to_not raise_exception
129
129
  end
130
130
  end
@@ -138,7 +138,7 @@ describe Shoulda::Matchers::ActiveModel::EnsureLengthOfMatcher do
138
138
 
139
139
  it "does not raise an exception" do
140
140
  expect {
141
- validating_length(:minimum => 4).should ensure_length_of(:attr).is_at_least(4)
141
+ expect(validating_length(minimum: 4)).to ensure_length_of(:attr).is_at_least(4)
142
142
  }.to_not raise_exception
143
143
  end
144
144
  end
@@ -152,14 +152,14 @@ describe Shoulda::Matchers::ActiveModel::EnsureLengthOfMatcher do
152
152
 
153
153
  it "does not raise an exception" do
154
154
  expect {
155
- validating_length(:is => 4).should ensure_length_of(:attr).is_equal_to(4)
155
+ expect(validating_length(is: 4)).to ensure_length_of(:attr).is_equal_to(4)
156
156
  }.to_not raise_exception
157
157
  end
158
158
  end
159
159
  end
160
160
 
161
161
  def validating_length(options = {})
162
- define_model(:example, :attr => :string) do
162
+ define_model(:example, attr: :string) do
163
163
  validates_length_of :attr, options
164
164
  end.new
165
165
  end
@@ -4,75 +4,75 @@ describe Shoulda::Matchers::ActiveModel::ExceptionMessageFinder do
4
4
  if active_model_3_2?
5
5
  context '#allow_description' do
6
6
  it 'describes its attribute' do
7
- finder = build_finder(:attribute => :attr)
7
+ finder = build_finder(attribute: :attr)
8
8
 
9
9
  description = finder.allow_description('allowed values')
10
10
 
11
- description.should eq %q(doesn't raise when attr is set to allowed values)
11
+ expect(description).to eq %q(doesn't raise when attr is set to allowed values)
12
12
  end
13
13
  end
14
14
 
15
15
  context '#expected_message_from' do
16
16
  it 'returns the message with the attribute name prefixed' do
17
- finder = build_finder(:attribute => :attr)
17
+ finder = build_finder(attribute: :attr)
18
18
 
19
19
  message = finder.expected_message_from('some message')
20
20
 
21
- message.should eq 'Attr some message'
21
+ expect(message).to eq 'Attr some message'
22
22
  end
23
23
  end
24
24
 
25
25
  context '#has_messages?' do
26
26
  it 'has messages when some validations fail' do
27
- finder = build_finder(:format => /abc/, :value => 'xyz')
27
+ finder = build_finder(format: /abc/, value: 'xyz')
28
28
 
29
29
  result = finder.has_messages?
30
30
 
31
- result.should be_true
31
+ expect(result).to eq true
32
32
  end
33
33
 
34
34
  it 'has no messages when all validations pass' do
35
- finder = build_finder(:format => /abc/, :value => 'abc')
35
+ finder = build_finder(format: /abc/, value: 'abc')
36
36
 
37
37
  result = finder.has_messages?
38
38
 
39
- result.should be_false
39
+ expect(result).to eq false
40
40
  end
41
41
  end
42
42
 
43
43
  context '#messages' do
44
44
  it 'returns errors for the given attribute' do
45
45
  finder = build_finder(
46
- :attribute => :attr,
47
- :format => /abc/,
48
- :value => 'xyz'
46
+ attribute: :attr,
47
+ format: /abc/,
48
+ value: 'xyz'
49
49
  )
50
50
 
51
51
  messages = finder.messages
52
52
 
53
- messages.should eq ['Attr is invalid']
53
+ expect(messages).to eq ['Attr is invalid']
54
54
  end
55
55
  end
56
56
 
57
57
  context '#messages_description' do
58
58
  it 'describes errors for the given attribute' do
59
59
  finder = build_finder(
60
- :attribute => :attr,
61
- :format => /abc/,
62
- :value => 'xyz'
60
+ attribute: :attr,
61
+ format: /abc/,
62
+ value: 'xyz'
63
63
  )
64
64
 
65
65
  description = finder.messages_description
66
66
 
67
- description.should eq 'Attr is invalid'
67
+ expect(description).to eq 'Attr is invalid'
68
68
  end
69
69
 
70
70
  it 'describes errors when there are none' do
71
- finder = build_finder(:format => /abc/, :value => 'abc')
71
+ finder = build_finder(format: /abc/, value: 'abc')
72
72
 
73
73
  description = finder.messages_description
74
74
 
75
- description.should eq 'no exception'
75
+ expect(description).to eq 'no exception'
76
76
  end
77
77
  end
78
78
 
@@ -82,7 +82,7 @@ describe Shoulda::Matchers::ActiveModel::ExceptionMessageFinder do
82
82
 
83
83
  description = finder.source_description
84
84
 
85
- description.should eq 'exception'
85
+ expect(description).to eq 'exception'
86
86
  end
87
87
  end
88
88
  end
@@ -103,7 +103,7 @@ describe Shoulda::Matchers::ActiveModel::ExceptionMessageFinder do
103
103
  def build_instance_validating(attribute, format, value)
104
104
  model_class = define_model(:example, attribute => :string) do
105
105
  attr_accessible attribute
106
- validates_format_of attribute, :with => format, :strict => true
106
+ validates_format_of attribute, with: format, strict: true
107
107
  end
108
108
 
109
109
  model_class.new(attribute => value)
@@ -23,13 +23,13 @@ describe Shoulda::Matchers::ActiveModel::Helpers do
23
23
  context 'if no translation for the model attribute’s error exists' do
24
24
  context 'and the translation for the model’s error exists' do
25
25
  it 'provides the right error message for validate_presence_of' do
26
- store_translations(:without => :model_attribute)
26
+ store_translations(without: :model_attribute)
27
27
 
28
28
  assert_presence_validation_has_correct_message
29
29
  end
30
30
 
31
31
  it 'provides the right error message for validates_length_of' do
32
- store_translations(:without => :model_attribute)
32
+ store_translations(without: :model_attribute)
33
33
 
34
34
  assert_length_validation_has_correct_message
35
35
  end
@@ -38,13 +38,13 @@ describe Shoulda::Matchers::ActiveModel::Helpers do
38
38
  context 'and no translation for the model’s error exists' do
39
39
  context 'and the translation for the message exists' do
40
40
  it 'provides the right error message for validate_presence_of' do
41
- store_translations(:without => [:model_attribute, :model])
41
+ store_translations(without: [:model_attribute, :model])
42
42
 
43
43
  assert_presence_validation_has_correct_message
44
44
  end
45
45
 
46
46
  it 'provides the right error message for validates_length_of' do
47
- store_translations(:without => [:model_attribute, :model])
47
+ store_translations(without: [:model_attribute, :model])
48
48
 
49
49
  assert_length_validation_has_correct_message
50
50
  end
@@ -53,13 +53,13 @@ describe Shoulda::Matchers::ActiveModel::Helpers do
53
53
  context 'and no translation for the message exists' do
54
54
  context 'and the translation for the attribute exists' do
55
55
  it 'provides the right error message for validate_presence_of' do
56
- store_translations(:without => [:model_attribute, :model, :message])
56
+ store_translations(without: [:model_attribute, :model, :message])
57
57
 
58
58
  assert_presence_validation_has_correct_message
59
59
  end
60
60
 
61
61
  it 'provides the right error message for validates_length_of' do
62
- store_translations(:without => [:model_attribute, :model, :message])
62
+ store_translations(without: [:model_attribute, :model, :message])
63
63
 
64
64
  assert_length_validation_has_correct_message
65
65
  end
@@ -80,46 +80,48 @@ describe Shoulda::Matchers::ActiveModel::Helpers do
80
80
 
81
81
  context 'if ActiveModel::Errors#generate_message behavior has changed' do
82
82
  it 'provides the right error message for validate_presence_of' do
83
- stub_active_model_message_generation(:type => :blank,
84
- :message => 'Behavior has diverged.')
83
+ stub_active_model_message_generation(type: :blank,
84
+ message: 'Behavior has diverged.')
85
85
  assert_presence_validation_has_correct_message
86
86
  end
87
87
  end
88
88
  end
89
89
 
90
90
  def assert_presence_validation_has_correct_message
91
- define_model :example, :attr => :string do
91
+ record = define_model :example, attr: :string do
92
92
  validates_presence_of :attr
93
- end.new.should validate_presence_of(:attr)
93
+ end.new
94
+ expect(record).to validate_presence_of(:attr)
94
95
  end
95
96
 
96
97
  def assert_length_validation_has_correct_message
97
- define_model :example, :attr => :string do
98
- validates_length_of :attr, :is => 40, :allow_blank => true
99
- end.new.should ensure_length_of(:attr).is_equal_to(40)
98
+ record = define_model :example, attr: :string do
99
+ validates_length_of :attr, is: 40, allow_blank: true
100
+ end.new
101
+ expect(record).to ensure_length_of(:attr).is_equal_to(40)
100
102
  end
101
103
 
102
- def store_translations(options = {:without => []})
104
+ def store_translations(options = {without: []})
103
105
  options[:without] = Array.wrap(options[:without] || [])
104
106
 
105
107
  translations = {
106
- :activerecord => {
107
- :errors => {
108
- :models => {
109
- :example => {
110
- :attributes => {
111
- :attr => {}
108
+ activerecord: {
109
+ errors: {
110
+ models: {
111
+ example: {
112
+ attributes: {
113
+ attr: {}
112
114
  }
113
115
  }
114
116
  },
115
- :messages => {}
117
+ messages: {}
116
118
  }
117
119
  },
118
- :errors => {
119
- :attributes => {
120
- :attr => {}
120
+ errors: {
121
+ attributes: {
122
+ attr: {}
121
123
  },
122
- :messages => {}
124
+ messages: {}
123
125
  }
124
126
  }
125
127
 
@@ -1,39 +1,152 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Shoulda::Matchers::ActiveModel::NumericalityMatchers::ComparisonMatcher do
4
- it_behaves_like 'a numerical submatcher' do
5
- subject { described_class.new(0, :>) }
4
+ subject { described_class.new(matcher, 0, :>) }
5
+
6
+ it_behaves_like 'a numerical submatcher'
7
+
8
+ context 'when initialized without correct numerical matcher' do
9
+ it 'raises an argument error' do
10
+ fake_matcher = matcher
11
+ class << fake_matcher
12
+ undef_method :diff_to_compare
13
+ end
14
+ expect do
15
+ described_class.new(fake_matcher, 0, :>)
16
+ end.to raise_error ArgumentError
17
+ end
6
18
  end
7
19
 
8
20
  context 'is_greater_than' do
9
- it { instance_with_validations(greater_than: 2).should matcher.is_greater_than(2) }
10
- it { instance_without_validations.should_not matcher.is_greater_than(2) }
21
+ it do
22
+ expect(instance_with_validations(greater_than: 2))
23
+ .to matcher.is_greater_than(2)
24
+ end
25
+
26
+ it do
27
+ expect(instance_with_validations(greater_than: 1.5))
28
+ .not_to matcher.is_greater_than(2)
29
+ end
30
+
31
+ it do
32
+ expect(instance_with_validations(greater_than: 2.5))
33
+ .not_to matcher.is_greater_than(2)
34
+ end
35
+
36
+ it do
37
+ expect(instance_without_validations).not_to matcher.is_greater_than(2)
38
+ end
11
39
  end
12
40
 
13
41
  context 'greater_than_or_equal_to' do
14
- it { instance_with_validations(greater_than_or_equal_to: 2).should matcher.is_greater_than_or_equal_to(2) }
15
- it { instance_without_validations.should_not matcher.is_greater_than_or_equal_to(2) }
42
+ it do
43
+ expect(instance_with_validations(greater_than_or_equal_to: 2))
44
+ .to matcher.is_greater_than_or_equal_to(2)
45
+ end
46
+
47
+ it do
48
+ expect(instance_with_validations(greater_than_or_equal_to: 1.5))
49
+ .not_to matcher.is_greater_than_or_equal_to(2)
50
+ end
51
+
52
+ it do
53
+ expect(instance_with_validations(greater_than_or_equal_to: 2.5))
54
+ .not_to matcher.is_greater_than_or_equal_to(2)
55
+ end
56
+
57
+ it do
58
+ expect(instance_without_validations)
59
+ .not_to matcher.is_greater_than_or_equal_to(2)
60
+ end
16
61
  end
17
62
 
18
63
  context 'less_than' do
19
- it { instance_with_validations(less_than: 2).should matcher.is_less_than(2) }
20
- it { instance_without_validations.should_not matcher.is_less_than(2) }
64
+ it do
65
+ expect(instance_with_validations(less_than: 2))
66
+ .to matcher.is_less_than(2)
67
+ end
68
+
69
+ it do
70
+ expect(instance_with_validations(less_than: 1.5))
71
+ .not_to matcher.is_less_than(2)
72
+ end
73
+
74
+ it do
75
+ expect(instance_with_validations(less_than: 2.5))
76
+ .not_to matcher.is_less_than(2)
77
+ end
78
+
79
+ it do
80
+ expect(instance_without_validations)
81
+ .not_to matcher.is_less_than(2)
82
+ end
21
83
  end
22
84
 
23
85
  context 'less_than_or_equal_to' do
24
- it { instance_with_validations(less_than_or_equal_to: 2).should matcher.is_less_than_or_equal_to(2) }
25
- it { instance_without_validations.should_not matcher.is_less_than_or_equal_to(2) }
86
+ it do
87
+ expect(instance_with_validations(less_than_or_equal_to: 2))
88
+ .to matcher.is_less_than_or_equal_to(2)
89
+ end
90
+
91
+ it do
92
+ expect(instance_with_validations(less_than_or_equal_to: 1.5))
93
+ .not_to matcher.is_less_than_or_equal_to(2)
94
+ end
95
+
96
+ it do
97
+ expect(instance_with_validations(less_than_or_equal_to: 2.5))
98
+ .not_to matcher.is_less_than_or_equal_to(2)
99
+ end
100
+
101
+ it do
102
+ expect(instance_without_validations)
103
+ .not_to matcher.is_less_than_or_equal_to(2)
104
+ end
26
105
  end
27
106
 
28
107
  context 'is_equal_to' do
29
- it { instance_with_validations(equal_to: 0).should matcher.is_equal_to(0) }
30
- it { instance_without_validations.should_not matcher.is_equal_to(0) }
108
+ it do
109
+ expect(instance_with_validations(equal_to: 0))
110
+ .to matcher.is_equal_to(0)
111
+ end
112
+
113
+ it do
114
+ expect(instance_with_validations(equal_to: -0.5))
115
+ .not_to matcher.is_equal_to(0)
116
+ end
117
+
118
+ it do
119
+ expect(instance_with_validations(equal_to: 0.5))
120
+ .not_to matcher.is_equal_to(0)
121
+ end
122
+
123
+ it do
124
+ expect(instance_without_validations)
125
+ .not_to matcher.is_equal_to(0)
126
+ end
31
127
  end
32
128
 
33
129
  context 'with_message' do
34
130
  it 'verifies the message for the validation' do
35
131
  instance = instance_with_validations(equal_to: 0, message: 'Must be zero')
36
- instance.should matcher.is_equal_to(0).with_message('Must be zero')
132
+ expect(instance).to matcher.is_equal_to(0).with_message('Must be zero')
133
+ end
134
+ end
135
+
136
+ describe '#comparison_description' do
137
+ [{ operator: :>, value: 0, expectation: 'greater than 0' },
138
+ { operator: :>=, value: -1.0, expectation: 'greater than or equal to -1.0' },
139
+ { operator: :==, value: 2.2, expectation: 'equal to 2.2' },
140
+ { operator: :<, value: -3, expectation: 'less than -3' },
141
+ { operator: :<=, value: 4, expectation: 'less than or equal to 4' },
142
+ ].each do |h|
143
+ context "with :#{h[:operator]} as operator and #{h[:value]} as value" do
144
+ subject do
145
+ described_class.new(matcher, h[:value], h[:operator])
146
+ .comparison_description
147
+ end
148
+ it { should eq h[:expectation] }
149
+ end
37
150
  end
38
151
  end
39
152