shoulda-matchers 4.0.0.rc1 → 4.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (209) hide show
  1. checksums.yaml +4 -4
  2. data/MIT-LICENSE +1 -1
  3. data/README.md +72 -40
  4. data/lib/shoulda/matchers/action_controller.rb +2 -0
  5. data/lib/shoulda/matchers/active_model.rb +2 -3
  6. data/lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb +1 -0
  7. data/lib/shoulda/matchers/active_record.rb +2 -0
  8. data/lib/shoulda/matchers/active_record/association_matcher.rb +34 -0
  9. data/lib/shoulda/matchers/active_record/association_matchers/optional_matcher.rb +27 -4
  10. data/lib/shoulda/matchers/active_record/association_matchers/required_matcher.rb +27 -4
  11. data/lib/shoulda/matchers/active_record/validate_uniqueness_of_matcher.rb +1 -1
  12. data/lib/shoulda/matchers/independent.rb +2 -1
  13. data/lib/shoulda/matchers/rails_shim.rb +5 -9
  14. data/lib/shoulda/matchers/version.rb +1 -1
  15. data/lib/shoulda/matchers/warn.rb +1 -0
  16. data/shoulda-matchers.gemspec +11 -3
  17. metadata +13 -340
  18. data/.gitignore +0 -12
  19. data/.hound.yml +0 -3
  20. data/.hound/ruby.yml +0 -1062
  21. data/.python-version +0 -1
  22. data/.rubocop.yml +0 -15
  23. data/.travis.yml +0 -21
  24. data/.yardopts +0 -10
  25. data/Appraisals +0 -105
  26. data/CONTRIBUTING.md +0 -172
  27. data/Gemfile +0 -15
  28. data/Gemfile.lock +0 -59
  29. data/MAINTAINING.md +0 -250
  30. data/NEWS.md +0 -1235
  31. data/Rakefile +0 -46
  32. data/bin/setup +0 -190
  33. data/custom_plan.rb +0 -104
  34. data/doc_config/gh-pages/index.html.erb +0 -9
  35. data/doc_config/yard/setup.rb +0 -22
  36. data/doc_config/yard/templates/default/fulldoc/html/css/bootstrap.css +0 -5967
  37. data/doc_config/yard/templates/default/fulldoc/html/css/full_list.css +0 -12
  38. data/doc_config/yard/templates/default/fulldoc/html/css/global.css +0 -66
  39. data/doc_config/yard/templates/default/fulldoc/html/css/solarized.css +0 -69
  40. data/doc_config/yard/templates/default/fulldoc/html/css/style.css +0 -312
  41. data/doc_config/yard/templates/default/fulldoc/html/full_list.erb +0 -26
  42. data/doc_config/yard/templates/default/fulldoc/html/full_list_class.erb +0 -1
  43. data/doc_config/yard/templates/default/fulldoc/html/full_list_method.erb +0 -8
  44. data/doc_config/yard/templates/default/fulldoc/html/js/app.js +0 -281
  45. data/doc_config/yard/templates/default/fulldoc/html/js/full_list.js +0 -1
  46. data/doc_config/yard/templates/default/fulldoc/html/js/jquery.stickyheaders.js +0 -289
  47. data/doc_config/yard/templates/default/fulldoc/html/js/underscore.min.js +0 -6
  48. data/doc_config/yard/templates/default/fulldoc/html/setup.rb +0 -35
  49. data/doc_config/yard/templates/default/layout/html/breadcrumb.erb +0 -14
  50. data/doc_config/yard/templates/default/layout/html/fonts.erb +0 -1
  51. data/doc_config/yard/templates/default/layout/html/footer.erb +0 -6
  52. data/doc_config/yard/templates/default/layout/html/layout.erb +0 -23
  53. data/doc_config/yard/templates/default/layout/html/search.erb +0 -13
  54. data/doc_config/yard/templates/default/layout/html/setup.rb +0 -40
  55. data/doc_config/yard/templates/default/method_details/html/source.erb +0 -10
  56. data/doc_config/yard/templates/default/module/html/box_info.erb +0 -31
  57. data/gemfiles/4.2.gemfile +0 -39
  58. data/gemfiles/4.2.gemfile.lock +0 -246
  59. data/gemfiles/5.0.gemfile +0 -37
  60. data/gemfiles/5.0.gemfile.lock +0 -238
  61. data/gemfiles/5.1.gemfile +0 -38
  62. data/gemfiles/5.1.gemfile.lock +0 -254
  63. data/gemfiles/5.2.gemfile +0 -40
  64. data/gemfiles/5.2.gemfile.lock +0 -273
  65. data/script/install_gems_in_all_appraisals +0 -16
  66. data/script/run_all_tests +0 -16
  67. data/script/supported_ruby_versions +0 -7
  68. data/script/update_gem_in_all_appraisals +0 -17
  69. data/script/update_gems_in_all_appraisals +0 -16
  70. data/spec/acceptance/active_model_integration_spec.rb +0 -23
  71. data/spec/acceptance/independent_matchers_spec.rb +0 -125
  72. data/spec/acceptance/multiple_libraries_integration_spec.rb +0 -55
  73. data/spec/acceptance/rails_integration_spec.rb +0 -156
  74. data/spec/acceptance_spec_helper.rb +0 -23
  75. data/spec/doublespeak_spec_helper.rb +0 -2
  76. data/spec/report_warnings.rb +0 -7
  77. data/spec/spec_helper.rb +0 -20
  78. data/spec/support/acceptance/adds_shoulda_matchers_to_project.rb +0 -133
  79. data/spec/support/acceptance/helpers.rb +0 -33
  80. data/spec/support/acceptance/helpers/active_model_helpers.rb +0 -11
  81. data/spec/support/acceptance/helpers/array_helpers.rb +0 -13
  82. data/spec/support/acceptance/helpers/base_helpers.rb +0 -19
  83. data/spec/support/acceptance/helpers/command_helpers.rb +0 -68
  84. data/spec/support/acceptance/helpers/file_helpers.rb +0 -19
  85. data/spec/support/acceptance/helpers/gem_helpers.rb +0 -31
  86. data/spec/support/acceptance/helpers/minitest_helpers.rb +0 -11
  87. data/spec/support/acceptance/helpers/n_unit_helpers.rb +0 -25
  88. data/spec/support/acceptance/helpers/pluralization_helpers.rb +0 -13
  89. data/spec/support/acceptance/helpers/rails_migration_helpers.rb +0 -21
  90. data/spec/support/acceptance/helpers/rails_version_helpers.rb +0 -11
  91. data/spec/support/acceptance/helpers/rspec_helpers.rb +0 -24
  92. data/spec/support/acceptance/helpers/ruby_version_helpers.rb +0 -9
  93. data/spec/support/acceptance/helpers/step_helpers.rb +0 -127
  94. data/spec/support/acceptance/matchers/have_output.rb +0 -31
  95. data/spec/support/acceptance/matchers/indicate_number_of_tests_was_run_matcher.rb +0 -55
  96. data/spec/support/acceptance/matchers/indicate_that_tests_were_run_matcher.rb +0 -103
  97. data/spec/support/tests/bundle.rb +0 -94
  98. data/spec/support/tests/command_runner.rb +0 -230
  99. data/spec/support/tests/current_bundle.rb +0 -55
  100. data/spec/support/tests/database.rb +0 -28
  101. data/spec/support/tests/database_adapters/postgresql.rb +0 -25
  102. data/spec/support/tests/database_adapters/sqlite3.rb +0 -26
  103. data/spec/support/tests/database_configuration.rb +0 -33
  104. data/spec/support/tests/database_configuration_registry.rb +0 -28
  105. data/spec/support/tests/filesystem.rb +0 -100
  106. data/spec/support/tests/version.rb +0 -45
  107. data/spec/support/unit/active_record/create_table.rb +0 -54
  108. data/spec/support/unit/attribute.rb +0 -45
  109. data/spec/support/unit/capture.rb +0 -46
  110. data/spec/support/unit/change_value.rb +0 -111
  111. data/spec/support/unit/create_model_arguments/basic.rb +0 -135
  112. data/spec/support/unit/create_model_arguments/has_many.rb +0 -15
  113. data/spec/support/unit/create_model_arguments/uniqueness_matcher.rb +0 -74
  114. data/spec/support/unit/helpers/action_pack_versions.rb +0 -22
  115. data/spec/support/unit/helpers/active_model_helpers.rb +0 -27
  116. data/spec/support/unit/helpers/active_model_versions.rb +0 -32
  117. data/spec/support/unit/helpers/active_record_versions.rb +0 -44
  118. data/spec/support/unit/helpers/active_resource_builder.rb +0 -27
  119. data/spec/support/unit/helpers/allow_value_matcher_helpers.rb +0 -15
  120. data/spec/support/unit/helpers/class_builder.rb +0 -90
  121. data/spec/support/unit/helpers/column_type_helpers.rb +0 -26
  122. data/spec/support/unit/helpers/confirmation_matcher_helpers.rb +0 -17
  123. data/spec/support/unit/helpers/controller_builder.rb +0 -63
  124. data/spec/support/unit/helpers/database_helpers.rb +0 -20
  125. data/spec/support/unit/helpers/i18n_faker.rb +0 -15
  126. data/spec/support/unit/helpers/mailer_builder.rb +0 -12
  127. data/spec/support/unit/helpers/message_helpers.rb +0 -19
  128. data/spec/support/unit/helpers/model_builder.rb +0 -114
  129. data/spec/support/unit/helpers/rails_versions.rb +0 -42
  130. data/spec/support/unit/helpers/validation_matcher_scenario_helpers.rb +0 -44
  131. data/spec/support/unit/i18n.rb +0 -7
  132. data/spec/support/unit/load_environment.rb +0 -12
  133. data/spec/support/unit/matchers/deprecate.rb +0 -60
  134. data/spec/support/unit/matchers/fail_with_message_including_matcher.rb +0 -51
  135. data/spec/support/unit/matchers/fail_with_message_matcher.rb +0 -64
  136. data/spec/support/unit/matchers/print_warning_including.rb +0 -67
  137. data/spec/support/unit/model_creation_strategies/active_model.rb +0 -111
  138. data/spec/support/unit/model_creation_strategies/active_record.rb +0 -77
  139. data/spec/support/unit/model_creators.rb +0 -19
  140. data/spec/support/unit/model_creators/active_model.rb +0 -39
  141. data/spec/support/unit/model_creators/active_record.rb +0 -42
  142. data/spec/support/unit/model_creators/active_record/has_and_belongs_to_many.rb +0 -95
  143. data/spec/support/unit/model_creators/active_record/has_many.rb +0 -67
  144. data/spec/support/unit/model_creators/active_record/uniqueness_matcher.rb +0 -42
  145. data/spec/support/unit/model_creators/basic.rb +0 -102
  146. data/spec/support/unit/rails_application.rb +0 -151
  147. data/spec/support/unit/record_builder_with_i18n_validation_message.rb +0 -69
  148. data/spec/support/unit/record_validating_confirmation_builder.rb +0 -54
  149. data/spec/support/unit/record_with_different_error_attribute_builder.rb +0 -92
  150. data/spec/support/unit/shared_examples/ignoring_interference_by_writer.rb +0 -79
  151. data/spec/support/unit/shared_examples/numerical_submatcher.rb +0 -17
  152. data/spec/support/unit/shared_examples/set_session_or_flash.rb +0 -360
  153. data/spec/support/unit/validation_matcher_scenario.rb +0 -60
  154. data/spec/unit/shoulda/matchers/action_controller/callback_matcher_spec.rb +0 -82
  155. data/spec/unit/shoulda/matchers/action_controller/filter_param_matcher_spec.rb +0 -28
  156. data/spec/unit/shoulda/matchers/action_controller/permit_matcher_spec.rb +0 -629
  157. data/spec/unit/shoulda/matchers/action_controller/redirect_to_matcher_spec.rb +0 -42
  158. data/spec/unit/shoulda/matchers/action_controller/render_template_matcher_spec.rb +0 -76
  159. data/spec/unit/shoulda/matchers/action_controller/render_with_layout_matcher_spec.rb +0 -62
  160. data/spec/unit/shoulda/matchers/action_controller/rescue_from_matcher_spec.rb +0 -90
  161. data/spec/unit/shoulda/matchers/action_controller/respond_with_matcher_spec.rb +0 -31
  162. data/spec/unit/shoulda/matchers/action_controller/route_matcher_spec.rb +0 -330
  163. data/spec/unit/shoulda/matchers/action_controller/route_params_spec.rb +0 -30
  164. data/spec/unit/shoulda/matchers/action_controller/set_flash_matcher_spec.rb +0 -67
  165. data/spec/unit/shoulda/matchers/action_controller/set_session_matcher_spec.rb +0 -17
  166. data/spec/unit/shoulda/matchers/action_controller/set_session_or_flash_matcher_spec.rb +0 -562
  167. data/spec/unit/shoulda/matchers/active_model/allow_mass_assignment_of_matcher_spec.rb +0 -117
  168. data/spec/unit/shoulda/matchers/active_model/allow_value_matcher_spec.rb +0 -829
  169. data/spec/unit/shoulda/matchers/active_model/disallow_value_matcher_spec.rb +0 -86
  170. data/spec/unit/shoulda/matchers/active_model/have_secure_password_matcher_spec.rb +0 -20
  171. data/spec/unit/shoulda/matchers/active_model/helpers_spec.rb +0 -162
  172. data/spec/unit/shoulda/matchers/active_model/validate_absence_of_matcher_spec.rb +0 -290
  173. data/spec/unit/shoulda/matchers/active_model/validate_acceptance_of_matcher_spec.rb +0 -109
  174. data/spec/unit/shoulda/matchers/active_model/validate_confirmation_of_matcher_spec.rb +0 -172
  175. data/spec/unit/shoulda/matchers/active_model/validate_exclusion_of_matcher_spec.rb +0 -264
  176. data/spec/unit/shoulda/matchers/active_model/validate_inclusion_of_matcher_spec.rb +0 -1049
  177. data/spec/unit/shoulda/matchers/active_model/validate_length_of_matcher_spec.rb +0 -335
  178. data/spec/unit/shoulda/matchers/active_model/validate_numericality_of_matcher_spec.rb +0 -1865
  179. data/spec/unit/shoulda/matchers/active_model/validate_presence_of_matcher_spec.rb +0 -406
  180. data/spec/unit/shoulda/matchers/active_record/accept_nested_attributes_for_matcher_spec.rb +0 -107
  181. data/spec/unit/shoulda/matchers/active_record/association_matcher_spec.rb +0 -1672
  182. data/spec/unit/shoulda/matchers/active_record/association_matchers/model_reflection_spec.rb +0 -251
  183. data/spec/unit/shoulda/matchers/active_record/define_enum_for_matcher_spec.rb +0 -690
  184. data/spec/unit/shoulda/matchers/active_record/have_db_column_matcher_spec.rb +0 -111
  185. data/spec/unit/shoulda/matchers/active_record/have_db_index_matcher_spec.rb +0 -85
  186. data/spec/unit/shoulda/matchers/active_record/have_readonly_attributes_matcher_spec.rb +0 -41
  187. data/spec/unit/shoulda/matchers/active_record/have_secure_token_matcher_spec.rb +0 -169
  188. data/spec/unit/shoulda/matchers/active_record/serialize_matcher_spec.rb +0 -86
  189. data/spec/unit/shoulda/matchers/active_record/validate_uniqueness_of_matcher_spec.rb +0 -1682
  190. data/spec/unit/shoulda/matchers/doublespeak/double_collection_spec.rb +0 -190
  191. data/spec/unit/shoulda/matchers/doublespeak/double_implementation_registry_spec.rb +0 -21
  192. data/spec/unit/shoulda/matchers/doublespeak/double_spec.rb +0 -271
  193. data/spec/unit/shoulda/matchers/doublespeak/object_double_spec.rb +0 -77
  194. data/spec/unit/shoulda/matchers/doublespeak/proxy_implementation_spec.rb +0 -72
  195. data/spec/unit/shoulda/matchers/doublespeak/stub_implementation_spec.rb +0 -101
  196. data/spec/unit/shoulda/matchers/doublespeak/world_spec.rb +0 -78
  197. data/spec/unit/shoulda/matchers/doublespeak_spec.rb +0 -27
  198. data/spec/unit/shoulda/matchers/independent/delegate_method_matcher/stubbed_target_spec.rb +0 -43
  199. data/spec/unit/shoulda/matchers/independent/delegate_method_matcher_spec.rb +0 -650
  200. data/spec/unit/shoulda/matchers/routing/route_matcher_spec.rb +0 -406
  201. data/spec/unit/shoulda/matchers/util/word_wrap_spec.rb +0 -252
  202. data/spec/unit_spec_helper.rb +0 -54
  203. data/spec/warnings_spy.rb +0 -64
  204. data/spec/warnings_spy/filesystem.rb +0 -45
  205. data/spec/warnings_spy/partitioner.rb +0 -36
  206. data/spec/warnings_spy/reader.rb +0 -53
  207. data/spec/warnings_spy/reporter.rb +0 -88
  208. data/tasks/documentation.rb +0 -199
  209. data/zeus.json +0 -11
@@ -1,406 +0,0 @@
1
- require 'unit_spec_helper'
2
-
3
- describe Shoulda::Matchers::ActiveModel::ValidatePresenceOfMatcher, type: :model do
4
- context 'a model with a presence validation' do
5
- it 'accepts' do
6
- expect(validating_presence).to matcher
7
- end
8
-
9
- it 'does not override the default message with a blank' do
10
- expect(validating_presence).to matcher.with_message(nil)
11
- end
12
-
13
- it_supports(
14
- 'ignoring_interference_by_writer',
15
- tests: {
16
- accept_if_qualified_but_changing_value_does_not_interfere: {
17
- changing_values_with: :nil_to_blank
18
- },
19
- reject_if_qualified_but_changing_value_interferes: {
20
- model_name: 'Example',
21
- attribute_name: :attr,
22
- changing_values_with: :never_falsy,
23
- expected_message: <<-MESSAGE
24
- Expected Example to validate that :attr cannot be empty/falsy, but this
25
- could not be proved.
26
- After setting :attr to ‹nil› -- which was read back as ‹"dummy value"›
27
- -- the matcher expected the Example to be invalid, but it was valid
28
- instead.
29
-
30
- As indicated in the message above, :attr seems to be changing certain
31
- values as they are set, and this could have something to do with why
32
- this test is failing. If you've overridden the writer method for this
33
- attribute, then you may need to change it to make this test pass, or
34
- do something else entirely.
35
- MESSAGE
36
- }
37
- }
38
- )
39
-
40
- it 'fails when used in the negative' do
41
- assertion = lambda do
42
- expect(validating_presence).not_to matcher
43
- end
44
-
45
- message = <<-MESSAGE
46
- Expected Example not to validate that :attr cannot be empty/falsy, but
47
- this could not be proved.
48
- After setting :attr to ‹nil›, the matcher expected the Example to be
49
- valid, but it was invalid instead, producing these validation errors:
50
-
51
- * attr: ["can't be blank"]
52
- MESSAGE
53
-
54
- expect(&assertion).to fail_with_message(message)
55
- end
56
- end
57
-
58
- context 'a model without a presence validation' do
59
- it 'rejects with the correct failure message' do
60
- record = define_model(:example, attr: :string).new
61
-
62
- assertion = lambda do
63
- expect(record).to matcher
64
- end
65
-
66
- message = <<-MESSAGE
67
- Expected Example to validate that :attr cannot be empty/falsy, but this
68
- could not be proved.
69
- After setting :attr to ‹nil›, the matcher expected the Example to be
70
- invalid, but it was valid instead.
71
- MESSAGE
72
-
73
- expect(&assertion).to fail_with_message(message)
74
- end
75
- end
76
-
77
- context 'an ActiveModel class with a presence validation' do
78
- it 'accepts' do
79
- expect(active_model_validating_presence).to matcher
80
- end
81
-
82
- it 'does not override the default message with a blank' do
83
- expect(active_model_validating_presence).to matcher.with_message(nil)
84
- end
85
-
86
- it_supports(
87
- 'ignoring_interference_by_writer',
88
- tests: {
89
- accept_if_qualified_but_changing_value_does_not_interfere: {
90
- changing_values_with: :nil_to_blank
91
- },
92
- reject_if_qualified_but_changing_value_interferes: {
93
- model_name: 'Example',
94
- attribute_name: :attr,
95
- changing_values_with: :never_falsy,
96
- expected_message: <<-MESSAGE
97
- Expected Example to validate that :attr cannot be empty/falsy, but this
98
- could not be proved.
99
- After setting :attr to ‹nil› -- which was read back as ‹"dummy value"›
100
- -- the matcher expected the Example to be invalid, but it was valid
101
- instead.
102
-
103
- As indicated in the message above, :attr seems to be changing certain
104
- values as they are set, and this could have something to do with why
105
- this test is failing. If you've overridden the writer method for this
106
- attribute, then you may need to change it to make this test pass, or
107
- do something else entirely.
108
- MESSAGE
109
- }
110
- }
111
- )
112
-
113
- def model_creator
114
- :active_model
115
- end
116
- end
117
-
118
- context 'an ActiveModel class without a presence validation' do
119
- it 'rejects with the correct failure message' do
120
- assertion = lambda do
121
- expect(active_model).to matcher
122
- end
123
-
124
- message = <<-MESSAGE
125
- Expected Example to validate that :attr cannot be empty/falsy, but this
126
- could not be proved.
127
- After setting :attr to ‹nil›, the matcher expected the Example to be
128
- invalid, but it was valid instead.
129
- MESSAGE
130
-
131
- expect(&assertion).to fail_with_message(message)
132
- end
133
- end
134
-
135
- context 'a has_many association with a presence validation' do
136
- it 'requires the attribute to be set' do
137
- expect(has_many_children(presence: true)).to validate_presence_of(:children)
138
- end
139
-
140
- it_supports(
141
- 'ignoring_interference_by_writer',
142
- tests: {
143
- accept_if_qualified_but_changing_value_does_not_interfere: {
144
- changing_values_with: :nil_to_blank
145
- },
146
- reject_if_qualified_but_changing_value_interferes: {
147
- model_name: 'Example',
148
- attribute_name: :attr,
149
- changing_values_with: :never_falsy,
150
- expected_message: <<-MESSAGE
151
- Expected Example to validate that :attr cannot be empty/falsy, but this
152
- could not be proved.
153
- After setting :attr to ‹nil› -- which was read back as ‹"dummy value"›
154
- -- the matcher expected the Example to be invalid, but it was valid
155
- instead.
156
-
157
- As indicated in the message above, :attr seems to be changing certain
158
- values as they are set, and this could have something to do with why
159
- this test is failing. If you've overridden the writer method for this
160
- attribute, then you may need to change it to make this test pass, or
161
- do something else entirely.
162
- MESSAGE
163
- }
164
- }
165
- )
166
-
167
- def model_creator
168
- :"active_record/has_many"
169
- end
170
- end
171
-
172
- context 'a has_many association without a presence validation' do
173
- it 'does not require the attribute to be set' do
174
- expect(has_many_children(presence: false)).
175
- not_to validate_presence_of(:children)
176
- end
177
- end
178
-
179
- context 'a required has_and_belongs_to_many association' do
180
- it 'accepts' do
181
- expect(build_record_having_and_belonging_to_many).
182
- to validate_presence_of(:children)
183
- end
184
-
185
- def build_record_having_and_belonging_to_many
186
- create_table 'children_parents', id: false do |t|
187
- t.integer :child_id
188
- t.integer :parent_id
189
- end
190
-
191
- define_model :child
192
-
193
- define_model :parent do
194
- has_and_belongs_to_many :children
195
- validates_presence_of :children
196
- end.new
197
- end
198
-
199
- it_supports(
200
- 'ignoring_interference_by_writer',
201
- tests: {
202
- accept_if_qualified_but_changing_value_does_not_interfere: {
203
- changing_values_with: :nil_to_blank
204
- },
205
- reject_if_qualified_but_changing_value_interferes: {
206
- model_name: 'Example',
207
- attribute_name: :attr,
208
- changing_values_with: :never_falsy,
209
- expected_message: <<-MESSAGE
210
- Expected Example to validate that :attr cannot be empty/falsy, but this
211
- could not be proved.
212
- After setting :attr to ‹nil› -- which was read back as ‹"dummy value"›
213
- -- the matcher expected the Example to be invalid, but it was valid
214
- instead.
215
-
216
- As indicated in the message above, :attr seems to be changing certain
217
- values as they are set, and this could have something to do with why
218
- this test is failing. If you've overridden the writer method for this
219
- attribute, then you may need to change it to make this test pass, or
220
- do something else entirely.
221
- MESSAGE
222
- }
223
- }
224
- )
225
-
226
- def model_creator
227
- :"active_record/has_and_belongs_to_many"
228
- end
229
- end
230
-
231
- context 'an optional has_and_belongs_to_many association' do
232
- before do
233
- define_model :child
234
- @model = define_model :parent do
235
- has_and_belongs_to_many :children
236
- end.new
237
- create_table 'children_parents', id: false do |t|
238
- t.integer :child_id
239
- t.integer :parent_id
240
- end
241
- end
242
-
243
- it 'rejects with the correct failure message' do
244
- assertion = lambda do
245
- expect(@model).to validate_presence_of(:children)
246
- end
247
-
248
- message = <<-MESSAGE
249
- Expected Parent to validate that :children cannot be empty/falsy, but
250
- this could not be proved.
251
- After setting :children to ‹[]›, the matcher expected the Parent to be
252
- invalid, but it was valid instead.
253
- MESSAGE
254
-
255
- expect(&assertion).to fail_with_message(message)
256
- end
257
- end
258
-
259
- context "an i18n translation containing %{attribute} and %{model}" do
260
- before do
261
- stub_translation(
262
- "activerecord.errors.messages.blank",
263
- "Please enter a %{attribute} for your %{model}")
264
- end
265
-
266
- after { I18n.backend.reload! }
267
-
268
- it "does not raise an exception" do
269
- expect {
270
- expect(validating_presence).to validate_presence_of(:attr)
271
- }.to_not raise_exception
272
- end
273
- end
274
-
275
- if active_model_3_2?
276
- context 'a strictly required attribute' do
277
- it 'accepts when the :strict options match' do
278
- expect(validating_presence(strict: true)).to matcher.strict
279
- end
280
-
281
- it 'rejects with the correct failure message when the :strict options do not match' do
282
- assertion = lambda do
283
- expect(validating_presence(strict: false)).to matcher.strict
284
- end
285
-
286
- message = <<-MESSAGE
287
- Expected Example to validate that :attr cannot be empty/falsy, raising a
288
- validation exception on failure, but this could not be proved.
289
- After setting :attr to ‹nil›, the matcher expected the Example to be
290
- invalid and to raise a validation exception, but the record produced
291
- validation errors instead.
292
- MESSAGE
293
-
294
- expect(&assertion).to fail_with_message(message)
295
- end
296
- end
297
-
298
- it 'does not override the default message with a blank' do
299
- expect(validating_presence(strict: true)).
300
- to matcher.strict.with_message(nil)
301
- end
302
- end
303
-
304
- context "an attribute with a context-dependent validation" do
305
- context "without the validation context" do
306
- it "does not match" do
307
- expect(validating_presence(on: :customisable)).not_to matcher
308
- end
309
- end
310
-
311
- context "with the validation context" do
312
- it "matches" do
313
- expect(validating_presence(on: :customisable)).to matcher.on(:customisable)
314
- end
315
- end
316
- end
317
-
318
- if rails_lte_4?
319
- context 'an active_resource model' do
320
- context 'with the validation context' do
321
- it 'does not raise an exception' do
322
- expect do
323
- expect(active_resource_model).to validate_presence_of(:attr)
324
- end.to_not raise_exception
325
- end
326
- end
327
- end
328
- end
329
-
330
- if rails_4_x?
331
- context 'against a pre-set password in a model that has_secure_password' do
332
- it 'raises a CouldNotSetPasswordError' do
333
- user_class = define_model :user, password_digest: :string do
334
- has_secure_password validations: false
335
- validates_presence_of :password
336
- end
337
-
338
- user = user_class.new
339
- user.password = 'something'
340
-
341
- assertion = lambda do
342
- expect(user).to validate_presence_of(:password)
343
- end
344
-
345
- expect(&assertion).to raise_error(
346
- Shoulda::Matchers::ActiveModel::CouldNotSetPasswordError
347
- )
348
- end
349
- end
350
- end
351
-
352
- context 'when the attribute typecasts nil to another blank value, such as an empty array' do
353
- it 'accepts (and does not raise an AttributeChangedValueError)' do
354
- model = define_active_model_class :example, accessors: [:foo] do
355
- validates_presence_of :foo
356
-
357
- def foo=(value)
358
- super(Array.wrap(value))
359
- end
360
- end
361
-
362
- expect(model.new).to validate_presence_of(:foo)
363
- end
364
- end
365
-
366
- def matcher
367
- validate_presence_of(:attr)
368
- end
369
-
370
- def validating_presence(options = {})
371
- define_model :example, attr: :string do
372
- validates_presence_of :attr, options
373
- end.new
374
- end
375
-
376
- def active_model(&block)
377
- define_active_model_class('Example', accessors: [:attr], &block).new
378
- end
379
-
380
- def active_model_validating_presence
381
- active_model { validates_presence_of :attr }
382
- end
383
-
384
- def has_many_children(options = {})
385
- define_model :child
386
- define_model :parent do
387
- has_many :children
388
- if options[:presence]
389
- validates_presence_of :children
390
- end
391
- end.new
392
- end
393
-
394
- def active_resource_model
395
- define_active_resource_class :foo, attr: :string do
396
- validates_presence_of :attr
397
- end.new
398
- end
399
-
400
- def validation_matcher_scenario_args
401
- super.deep_merge(
402
- matcher_name: :validate_presence_of,
403
- model_creator: :active_record
404
- )
405
- end
406
- end
@@ -1,107 +0,0 @@
1
- require 'unit_spec_helper'
2
-
3
- describe Shoulda::Matchers::ActiveRecord::AcceptNestedAttributesForMatcher, type: :model do
4
- it 'accepts an existing declaration' do
5
- expect(accepting_children).to accept_nested_attributes_for(:children)
6
- end
7
-
8
- it 'rejects a missing declaration' do
9
- matcher = children_matcher
10
-
11
- expect(matcher.matches?(rejecting_children)).to eq false
12
-
13
- expect(matcher.failure_message).
14
- to eq 'Expected Parent to accept nested attributes for children (is not declared)'
15
- end
16
-
17
- context 'allow_destroy' do
18
- it 'accepts a valid truthy value' do
19
- matching = accepting_children(allow_destroy: true)
20
-
21
- expect(matching).to children_matcher.allow_destroy(true)
22
- end
23
-
24
- it 'accepts a valid falsey value' do
25
- matching = accepting_children(allow_destroy: false)
26
-
27
- expect(matching).to children_matcher.allow_destroy(false)
28
- end
29
-
30
- it 'rejects an invalid truthy value' do
31
- matcher = children_matcher
32
- matching = accepting_children(allow_destroy: true)
33
-
34
- expect(matcher.allow_destroy(false).matches?(matching)).to eq false
35
- expect(matcher.failure_message).to match(/should not allow destroy/)
36
- end
37
-
38
- it 'rejects an invalid falsey value' do
39
- matcher = children_matcher
40
- matching = accepting_children(allow_destroy: false)
41
-
42
- expect(matcher.allow_destroy(true).matches?(matching)).to eq false
43
- expect(matcher.failure_message).to match(/should allow destroy/)
44
- end
45
- end
46
-
47
- context 'limit' do
48
- it 'accepts a correct value' do
49
- expect(accepting_children(limit: 3)).to children_matcher.limit(3)
50
- end
51
-
52
- it 'rejects a false value' do
53
- matcher = children_matcher
54
- rejecting = accepting_children(limit: 3)
55
-
56
- expect(matcher.limit(2).matches?(rejecting)).to eq false
57
- expect(matcher.failure_message).to match(/limit should be 2, got 3/)
58
- end
59
- end
60
-
61
- context 'update_only' do
62
- it 'accepts a valid truthy value' do
63
- expect(accepting_children(update_only: true)).
64
- to children_matcher.update_only(true)
65
- end
66
-
67
- it 'accepts a valid falsey value' do
68
- expect(accepting_children(update_only: false)).
69
- to children_matcher.update_only(false)
70
- end
71
-
72
- it 'rejects an invalid truthy value' do
73
- matcher = children_matcher.update_only(false)
74
- rejecting = accepting_children(update_only: true)
75
-
76
- expect(matcher.matches?(rejecting)).to eq false
77
- expect(matcher.failure_message).to match(/should not be update only/)
78
- end
79
-
80
- it 'rejects an invalid falsey value' do
81
- matcher = children_matcher.update_only(true)
82
- rejecting = accepting_children(update_only: false)
83
-
84
- expect(matcher.matches?(rejecting)).to eq false
85
- expect(matcher.failure_message).to match(/should be update only/)
86
- end
87
- end
88
-
89
- def accepting_children(options = {})
90
- define_model :child, parent_id: :integer
91
- define_model :parent do
92
- has_many :children
93
- accepts_nested_attributes_for :children, options
94
- end.new
95
- end
96
-
97
- def children_matcher
98
- accept_nested_attributes_for(:children)
99
- end
100
-
101
- def rejecting_children
102
- define_model :child, parent_id: :integer
103
- define_model :parent do
104
- has_many :children
105
- end.new
106
- end
107
- end