shoulda-matchers 1.4.2 → 1.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (135) hide show
  1. data/.travis.yml +11 -3
  2. data/Appraisals +3 -3
  3. data/Gemfile +1 -1
  4. data/Gemfile.lock +69 -70
  5. data/MIT-LICENSE +1 -1
  6. data/NEWS.md +22 -4
  7. data/README.md +3 -1
  8. data/Rakefile +9 -5
  9. data/features/step_definitions/rails_steps.rb +7 -7
  10. data/features/support/env.rb +1 -1
  11. data/gemfiles/3.0.gemfile +1 -1
  12. data/gemfiles/3.0.gemfile.lock +25 -22
  13. data/gemfiles/3.1.gemfile +1 -1
  14. data/gemfiles/3.1.gemfile.lock +27 -24
  15. data/gemfiles/3.2.gemfile +1 -1
  16. data/gemfiles/3.2.gemfile.lock +56 -52
  17. data/lib/shoulda/matchers/action_controller.rb +3 -2
  18. data/lib/shoulda/matchers/action_controller/assign_to_matcher.rb +10 -8
  19. data/lib/shoulda/matchers/action_controller/filter_param_matcher.rb +2 -4
  20. data/lib/shoulda/matchers/action_controller/redirect_to_matcher.rb +3 -4
  21. data/lib/shoulda/matchers/action_controller/render_template_matcher.rb +3 -4
  22. data/lib/shoulda/matchers/action_controller/render_with_layout_matcher.rb +6 -6
  23. data/lib/shoulda/matchers/action_controller/respond_with_content_type_matcher.rb +5 -2
  24. data/lib/shoulda/matchers/action_controller/respond_with_matcher.rb +2 -2
  25. data/lib/shoulda/matchers/action_controller/route_matcher.rb +12 -12
  26. data/lib/shoulda/matchers/action_controller/set_session_matcher.rb +3 -4
  27. data/lib/shoulda/matchers/action_controller/set_the_flash_matcher.rb +10 -11
  28. data/lib/shoulda/matchers/action_controller/strong_parameters_matcher.rb +121 -0
  29. data/lib/shoulda/matchers/action_mailer/have_sent_email_matcher.rb +7 -4
  30. data/lib/shoulda/matchers/active_model.rb +1 -1
  31. data/lib/shoulda/matchers/active_model/allow_mass_assignment_of_matcher.rb +8 -8
  32. data/lib/shoulda/matchers/active_model/allow_value_matcher.rb +5 -4
  33. data/lib/shoulda/matchers/active_model/disallow_value_matcher.rb +3 -3
  34. data/lib/shoulda/matchers/active_model/ensure_inclusion_of_matcher.rb +2 -2
  35. data/lib/shoulda/matchers/active_model/exception_message_finder.rb +0 -1
  36. data/lib/shoulda/matchers/active_model/helpers.rb +16 -7
  37. data/lib/shoulda/matchers/active_model/only_integer_matcher.rb +3 -3
  38. data/lib/shoulda/matchers/active_model/validate_confirmation_of_matcher.rb +8 -6
  39. data/lib/shoulda/matchers/active_model/validate_format_of_matcher.rb +19 -10
  40. data/lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb +4 -4
  41. data/lib/shoulda/matchers/active_model/validate_uniqueness_of_matcher.rb +19 -21
  42. data/lib/shoulda/matchers/active_model/validation_matcher.rb +7 -7
  43. data/lib/shoulda/matchers/active_model/validation_message_finder.rb +1 -1
  44. data/lib/shoulda/matchers/active_record/accept_nested_attributes_for_matcher.rb +5 -5
  45. data/lib/shoulda/matchers/active_record/association_matcher.rb +3 -3
  46. data/lib/shoulda/matchers/active_record/have_db_column_matcher.rb +11 -7
  47. data/lib/shoulda/matchers/active_record/have_db_index_matcher.rb +2 -2
  48. data/lib/shoulda/matchers/active_record/have_readonly_attribute_matcher.rb +5 -5
  49. data/lib/shoulda/matchers/active_record/query_the_database_matcher.rb +5 -2
  50. data/lib/shoulda/matchers/active_record/serialize_matcher.rb +3 -3
  51. data/lib/shoulda/matchers/assertion_error.rb +4 -1
  52. data/lib/shoulda/matchers/independent/delegate_matcher.rb +17 -7
  53. data/lib/shoulda/matchers/integrations/rspec.rb +19 -33
  54. data/lib/shoulda/matchers/version.rb +1 -1
  55. data/shoulda-matchers.gemspec +6 -4
  56. data/spec/shoulda/matchers/action_controller/assign_to_matcher_spec.rb +66 -0
  57. data/spec/shoulda/matchers/action_controller/filter_param_matcher_spec.rb +22 -0
  58. data/spec/shoulda/matchers/action_controller/redirect_to_matcher_spec.rb +42 -0
  59. data/spec/shoulda/matchers/action_controller/render_template_matcher_spec.rb +78 -0
  60. data/spec/shoulda/matchers/action_controller/render_with_layout_matcher_spec.rb +58 -0
  61. data/spec/shoulda/matchers/action_controller/respond_with_content_type_matcher_spec.rb +31 -0
  62. data/spec/shoulda/matchers/action_controller/respond_with_matcher_spec.rb +31 -0
  63. data/spec/shoulda/matchers/action_controller/route_matcher_spec.rb +65 -0
  64. data/spec/shoulda/matchers/action_controller/set_session_matcher_spec.rb +51 -0
  65. data/spec/shoulda/matchers/action_controller/set_the_flash_matcher_spec.rb +153 -0
  66. data/spec/shoulda/matchers/action_controller/strong_parameters_matcher_spec.rb +142 -0
  67. data/spec/shoulda/matchers/action_mailer/have_sent_email_spec.rb +324 -0
  68. data/spec/shoulda/matchers/active_model/allow_mass_assignment_of_matcher_spec.rb +111 -0
  69. data/spec/shoulda/matchers/active_model/allow_value_matcher_spec.rb +124 -0
  70. data/spec/shoulda/matchers/active_model/disallow_value_matcher_spec.rb +43 -0
  71. data/spec/shoulda/matchers/active_model/ensure_exclusion_of_matcher_spec.rb +74 -0
  72. data/spec/shoulda/matchers/active_model/ensure_inclusion_of_matcher_spec.rb +171 -0
  73. data/spec/shoulda/matchers/active_model/ensure_length_of_matcher_spec.rb +113 -0
  74. data/spec/shoulda/{active_model → matchers/active_model}/exception_message_finder_spec.rb +2 -2
  75. data/spec/shoulda/matchers/active_model/helpers_spec.rb +158 -0
  76. data/spec/shoulda/matchers/active_model/only_integer_matcher_spec.rb +52 -0
  77. data/spec/shoulda/matchers/active_model/validate_acceptance_of_matcher_spec.rb +41 -0
  78. data/spec/shoulda/matchers/active_model/validate_confirmation_of_matcher_spec.rb +41 -0
  79. data/spec/shoulda/matchers/active_model/validate_format_of_matcher_spec.rb +75 -0
  80. data/spec/shoulda/matchers/active_model/validate_numericality_of_matcher_spec.rb +73 -0
  81. data/spec/shoulda/matchers/active_model/validate_presence_of_matcher_spec.rb +127 -0
  82. data/spec/shoulda/matchers/active_model/validate_uniqueness_of_matcher_spec.rb +175 -0
  83. data/spec/shoulda/{active_model → matchers/active_model}/validation_message_finder_spec.rb +0 -1
  84. data/spec/shoulda/matchers/active_record/accept_nested_attributes_for_matcher_spec.rb +106 -0
  85. data/spec/shoulda/matchers/active_record/association_matcher_spec.rb +534 -0
  86. data/spec/shoulda/matchers/active_record/have_db_column_matcher_spec.rb +111 -0
  87. data/spec/shoulda/matchers/active_record/have_db_index_matcher_spec.rb +78 -0
  88. data/spec/shoulda/matchers/active_record/have_readonly_attributes_matcher_spec.rb +41 -0
  89. data/spec/shoulda/{active_record → matchers/active_record}/query_the_database_matcher_spec.rb +10 -10
  90. data/spec/shoulda/matchers/active_record/serialize_matcher_spec.rb +86 -0
  91. data/spec/shoulda/{independent → matchers/independent}/delegate_matcher_spec.rb +55 -34
  92. data/spec/spec_helper.rb +1 -2
  93. data/spec/support/active_model_versions.rb +4 -0
  94. data/spec/support/activemodel_helpers.rb +19 -0
  95. data/spec/support/controller_builder.rb +38 -12
  96. data/spec/support/model_builder.rb +8 -2
  97. metadata +106 -98
  98. data/spec/fixtures/addresses.yml +0 -3
  99. data/spec/fixtures/friendships.yml +0 -0
  100. data/spec/fixtures/posts.yml +0 -5
  101. data/spec/fixtures/products.yml +0 -0
  102. data/spec/fixtures/taggings.yml +0 -0
  103. data/spec/fixtures/tags.yml +0 -9
  104. data/spec/fixtures/users.yml +0 -6
  105. data/spec/shoulda/action_controller/assign_to_matcher_spec.rb +0 -63
  106. data/spec/shoulda/action_controller/filter_param_matcher_spec.rb +0 -20
  107. data/spec/shoulda/action_controller/redirect_to_matcher_spec.rb +0 -40
  108. data/spec/shoulda/action_controller/render_template_matcher_spec.rb +0 -69
  109. data/spec/shoulda/action_controller/render_with_layout_matcher_spec.rb +0 -55
  110. data/spec/shoulda/action_controller/respond_with_content_type_matcher_spec.rb +0 -28
  111. data/spec/shoulda/action_controller/respond_with_matcher_spec.rb +0 -83
  112. data/spec/shoulda/action_controller/route_matcher_spec.rb +0 -65
  113. data/spec/shoulda/action_controller/set_session_matcher_spec.rb +0 -46
  114. data/spec/shoulda/action_controller/set_the_flash_matcher_spec.rb +0 -130
  115. data/spec/shoulda/action_mailer/have_sent_email_spec.rb +0 -333
  116. data/spec/shoulda/active_model/allow_mass_assignment_of_matcher_spec.rb +0 -115
  117. data/spec/shoulda/active_model/allow_value_matcher_spec.rb +0 -131
  118. data/spec/shoulda/active_model/disallow_value_matcher_spec.rb +0 -65
  119. data/spec/shoulda/active_model/ensure_exclusion_of_matcher_spec.rb +0 -79
  120. data/spec/shoulda/active_model/ensure_inclusion_of_matcher_spec.rb +0 -181
  121. data/spec/shoulda/active_model/ensure_length_of_matcher_spec.rb +0 -138
  122. data/spec/shoulda/active_model/helpers_spec.rb +0 -129
  123. data/spec/shoulda/active_model/only_integer_matcher_spec.rb +0 -69
  124. data/spec/shoulda/active_model/validate_acceptance_of_matcher_spec.rb +0 -43
  125. data/spec/shoulda/active_model/validate_confirmation_of_matcher_spec.rb +0 -48
  126. data/spec/shoulda/active_model/validate_format_of_matcher_spec.rb +0 -79
  127. data/spec/shoulda/active_model/validate_numericality_of_matcher_spec.rb +0 -112
  128. data/spec/shoulda/active_model/validate_presence_of_matcher_spec.rb +0 -135
  129. data/spec/shoulda/active_model/validate_uniqueness_of_matcher_spec.rb +0 -154
  130. data/spec/shoulda/active_record/accept_nested_attributes_for_matcher_spec.rb +0 -84
  131. data/spec/shoulda/active_record/association_matcher_spec.rb +0 -642
  132. data/spec/shoulda/active_record/have_db_column_matcher_spec.rb +0 -185
  133. data/spec/shoulda/active_record/have_db_index_matcher_spec.rb +0 -105
  134. data/spec/shoulda/active_record/have_readonly_attributes_matcher_spec.rb +0 -46
  135. data/spec/shoulda/active_record/serialize_matcher_spec.rb +0 -81
@@ -1,642 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Shoulda::Matchers::ActiveRecord::AssociationMatcher do
4
- context "belong_to" do
5
- before do
6
- @matcher = belong_to(:parent)
7
- end
8
-
9
- it "should accept a good association with the default foreign key" do
10
- define_model :parent
11
- define_model :child, :parent_id => :integer do
12
- belongs_to :parent
13
- end
14
- Child.new.should @matcher
15
- end
16
-
17
- it "should reject a nonexistent association" do
18
- define_model :child
19
- Child.new.should_not @matcher
20
- end
21
-
22
- it "should reject an association of the wrong type" do
23
- define_model :parent, :child_id => :integer
24
- child_class = define_model :child do
25
- has_one :parent
26
- end
27
- Child.new.should_not @matcher
28
- end
29
-
30
- it "should reject an association that has a nonexistent foreign key" do
31
- define_model :parent
32
- define_model :child do
33
- belongs_to :parent
34
- end
35
- Child.new.should_not @matcher
36
- end
37
-
38
- it "should accept an association with an existing custom foreign key" do
39
- define_model :parent
40
- define_model :child, :guardian_id => :integer do
41
- belongs_to :parent, :foreign_key => 'guardian_id'
42
- end
43
- Child.new.should @matcher
44
- end
45
-
46
- it "should accept a polymorphic association" do
47
- define_model :child, :parent_type => :string,
48
- :parent_id => :integer do
49
- belongs_to :parent, :polymorphic => true
50
- end
51
- Child.new.should @matcher
52
- end
53
-
54
- it "should accept an association with a valid :dependent option" do
55
- define_model :parent
56
- define_model :child, :parent_id => :integer do
57
- belongs_to :parent, :dependent => :destroy
58
- end
59
- Child.new.should @matcher.dependent(:destroy)
60
- end
61
-
62
- it "should reject an association with a bad :dependent option" do
63
- define_model :parent
64
- define_model :child, :parent_id => :integer do
65
- belongs_to :parent
66
- end
67
- Child.new.should_not @matcher.dependent(:destroy)
68
- end
69
-
70
- it "should accept an association with a valid :conditions option" do
71
- define_model :parent, :adopter => :boolean
72
- define_model :child, :parent_id => :integer do
73
- belongs_to :parent, :conditions => { :adopter => true }
74
- end
75
- Child.new.should @matcher.conditions(:adopter => true)
76
- end
77
-
78
- it "should reject an association with a bad :conditions option" do
79
- define_model :parent, :adopter => :boolean
80
- define_model :child, :parent_id => :integer do
81
- belongs_to :parent
82
- end
83
- Child.new.should_not @matcher.conditions(:adopter => true)
84
- end
85
-
86
- it "should accept an association with a valid :class_name option" do
87
- define_model :tree_parent, :adopter => :boolean
88
- define_model :child, :parent_id => :integer do
89
- belongs_to :parent, :class_name => 'TreeParent'
90
- end
91
- Child.new.should @matcher.class_name('TreeParent')
92
- end
93
-
94
- it "should reject an association with a bad :class_name option" do
95
- define_model :parent, :adopter => :boolean
96
- define_model :child, :parent_id => :integer do
97
- belongs_to :parent
98
- end
99
- Child.new.should_not @matcher.class_name('TreeChild')
100
- end
101
-
102
- context 'should accept an association with a false :validate option' do
103
- before do
104
- define_model :parent, :adopter => :boolean
105
- define_model :child, :parent_id => :integer do
106
- belongs_to :parent, :validate => false
107
- end
108
- end
109
- subject { Child.new }
110
- specify { subject.should @matcher.validate(false) }
111
- specify { subject.should_not @matcher.validate(true) }
112
- specify { subject.should_not @matcher.validate }
113
- end
114
-
115
- context 'should accept an association with a true :validate option' do
116
- before do
117
- define_model :parent, :adopter => :boolean
118
- define_model :child, :parent_id => :integer do
119
- belongs_to :parent, :validate => true
120
- end
121
- end
122
- subject { Child.new }
123
- specify { subject.should_not @matcher.validate(false) }
124
- specify { subject.should @matcher.validate(true) }
125
- specify { subject.should @matcher.validate }
126
- end
127
-
128
- context 'should accept an association without a :validate option' do
129
- before do
130
- define_model :parent, :adopter => :boolean
131
- define_model :child, :parent_id => :integer do
132
- belongs_to :parent
133
- end
134
- end
135
- subject { Child.new }
136
- specify { subject.should @matcher.validate(false) }
137
- specify { subject.should_not @matcher.validate(true) }
138
- specify { subject.should_not @matcher.validate }
139
- end
140
-
141
- end
142
-
143
- context "have_many" do
144
- before do
145
- @matcher = have_many(:children)
146
- end
147
-
148
- it "should accept a valid association without any options" do
149
- define_model :child, :parent_id => :integer
150
- define_model :parent do
151
- has_many :children
152
- end
153
- Parent.new.should @matcher
154
- end
155
-
156
- it "should accept a valid association with a :through option" do
157
- define_model :child
158
- define_model :conception, :child_id => :integer,
159
- :parent_id => :integer do
160
- belongs_to :child
161
- end
162
- define_model :parent do
163
- has_many :conceptions
164
- has_many :children, :through => :conceptions
165
- end
166
- Parent.new.should @matcher
167
- end
168
-
169
- it "should accept a valid association with an :as option" do
170
- define_model :child, :guardian_type => :string,
171
- :guardian_id => :integer
172
- define_model :parent do
173
- has_many :children, :as => :guardian
174
- end
175
- Parent.new.should @matcher
176
- end
177
-
178
- it "should reject an association that has a nonexistent foreign key" do
179
- define_model :child
180
- define_model :parent do
181
- has_many :children
182
- end
183
- Parent.new.should_not @matcher
184
- end
185
-
186
- it "should reject an association with a bad :as option" do
187
- define_model :child, :caretaker_type => :string,
188
- :caretaker_id => :integer
189
- define_model :parent do
190
- has_many :children, :as => :guardian
191
- end
192
- Parent.new.should_not @matcher
193
- end
194
-
195
- it "should reject an association that has a bad :through option" do
196
- define_model :child, :parent_id => :integer
197
- define_model :parent do
198
- has_many :children
199
- end
200
- @matcher.through(:conceptions).matches?(Parent.new).should be_false
201
- @matcher.failure_message.should =~ /does not have any relationship to conceptions/
202
- end
203
-
204
- it "should reject an association that has the wrong :through option" do
205
- define_model :child
206
- define_model :conception, :child_id => :integer,
207
- :parent_id => :integer do
208
- belongs_to :child
209
- end
210
- define_model :parent do
211
- has_many :conceptions
212
- has_many :relationships
213
- has_many :children, :through => :conceptions
214
- end
215
- @matcher.through(:relationships).matches?(Parent.new).should be_false
216
- @matcher.failure_message.should =~ /through relationships, but got it through conceptions/
217
- end
218
-
219
- it "should accept an association with a valid :dependent option" do
220
- define_model :child, :parent_id => :integer
221
- define_model :parent do
222
- has_many :children, :dependent => :destroy
223
- end
224
- Parent.new.should @matcher.dependent(:destroy)
225
- end
226
-
227
- it "should reject an association with a bad :dependent option" do
228
- define_model :child, :parent_id => :integer
229
- define_model :parent do
230
- has_many :children
231
- end
232
- Parent.new.should_not @matcher.dependent(:destroy)
233
- end
234
-
235
- it "should accept an association with a valid :order option" do
236
- define_model :child, :parent_id => :integer
237
- define_model :parent do
238
- has_many :children, :order => :id
239
- end
240
- Parent.new.should @matcher.order(:id)
241
- end
242
-
243
- it "should reject an association with a bad :order option" do
244
- define_model :child, :parent_id => :integer
245
- define_model :parent do
246
- has_many :children
247
- end
248
- Parent.new.should_not @matcher.order(:id)
249
- end
250
-
251
- it "should accept an association with a valid :conditions option" do
252
- define_model :child, :parent_id => :integer, :adopted => :boolean
253
- define_model :parent do
254
- has_many :children, :conditions => { :adopted => true }
255
- end
256
- Parent.new.should @matcher.conditions({ :adopted => true })
257
- end
258
-
259
- it "should reject an association with a bad :conditions option" do
260
- define_model :child, :parent_id => :integer, :adopted => :boolean
261
- define_model :parent do
262
- has_many :children
263
- end
264
- Parent.new.should_not @matcher.conditions({ :adopted => true })
265
- end
266
-
267
- it "should accept an association with a valid :class_name option" do
268
- define_model :node, :parent_id => :integer, :adopted => :boolean
269
- define_model :parent do
270
- has_many :children, :class_name => 'Node'
271
- end
272
- Parent.new.should @matcher.class_name('Node')
273
- end
274
-
275
- it "should reject an association with a bad :class_name option" do
276
- define_model :child, :parent_id => :integer, :adopted => :boolean
277
- define_model :parent do
278
- has_many :children
279
- end
280
- Parent.new.should_not @matcher.class_name('Node')
281
- end
282
-
283
- context 'should accept an association with a false :validate option' do
284
- before do
285
- define_model :child, :parent_id => :integer, :adopted => :boolean
286
- define_model :parent do
287
- has_many :children, :validate => false
288
- end
289
- end
290
- subject { Parent.new }
291
- specify { subject.should @matcher.validate(false) }
292
- specify { subject.should_not @matcher.validate(true) }
293
- specify { subject.should_not @matcher.validate }
294
- end
295
-
296
- context 'should accept an association with a true :validate option' do
297
- before do
298
- define_model :child, :parent_id => :integer, :adopted => :boolean
299
- define_model :parent do
300
- has_many :children, :validate => true
301
- end
302
- end
303
- subject { Parent.new }
304
- specify { subject.should_not @matcher.validate(false) }
305
- specify { subject.should @matcher.validate(true) }
306
- specify { subject.should @matcher.validate }
307
- end
308
-
309
- context 'should accept an association without a :validate option' do
310
- before do
311
- define_model :child, :parent_id => :integer, :adopted => :boolean
312
- define_model :parent do
313
- has_many :children
314
- end
315
- end
316
- subject { Parent.new }
317
- specify { subject.should @matcher.validate(false) }
318
- specify { subject.should_not @matcher.validate(true) }
319
- specify { subject.should_not @matcher.validate }
320
- end
321
-
322
- it "should accept an association with a nonstandard reverse foreign key, using :inverse_of" do
323
- define_model :child, :ancestor_id => :integer do
324
- belongs_to :ancestor, :inverse_of => :children, :class_name => :Parent
325
- end
326
- define_model :parent do
327
- has_many :children, :inverse_of => :ancestor
328
- end
329
- Parent.new.should @matcher
330
- end
331
-
332
- it "should reject an association with a nonstandard reverse foreign key, if :inverse_of is not correct" do
333
- define_model :child, :mother_id => :integer do
334
- belongs_to :mother, :inverse_of => :children, :class_name => :Parent
335
- end
336
- define_model :parent do
337
- has_many :children, :inverse_of => :ancestor
338
- end
339
- Parent.new.should_not @matcher
340
- end
341
- end
342
-
343
- context "have_one" do
344
- before do
345
- @matcher = have_one(:detail)
346
- end
347
-
348
- it "should accept a valid association without any options" do
349
- define_model :detail, :person_id => :integer
350
- define_model :person do
351
- has_one :detail
352
- end
353
- Person.new.should @matcher
354
- end
355
-
356
- it "should accept a valid association with an :as option" do
357
- define_model :detail, :detailable_id => :integer,
358
- :detailable_type => :string
359
- define_model :person do
360
- has_one :detail, :as => :detailable
361
- end
362
- Person.new.should @matcher
363
- end
364
-
365
- it "should reject an association that has a nonexistent foreign key" do
366
- define_model :detail
367
- define_model :person do
368
- has_one :detail
369
- end
370
- Person.new.should_not @matcher
371
- end
372
-
373
- it "should accept an association with an existing custom foreign key" do
374
- define_model :detail, :detailed_person_id => :integer
375
- define_model :person do
376
- has_one :detail, :foreign_key => :detailed_person_id
377
- end
378
- Person.new.should @matcher.with_foreign_key(:detailed_person_id)
379
- end
380
-
381
- it "should reject an association with a bad :as option" do
382
- define_model :detail, :detailable_id => :integer,
383
- :detailable_type => :string
384
- define_model :person do
385
- has_one :detail, :as => :describable
386
- end
387
- Person.new.should_not @matcher
388
- end
389
-
390
- it "should accept an association with a valid :dependent option" do
391
- define_model :detail, :person_id => :integer
392
- define_model :person do
393
- has_one :detail, :dependent => :destroy
394
- end
395
- Person.new.should @matcher.dependent(:destroy)
396
- end
397
-
398
- it "should reject an association with a bad :dependent option" do
399
- define_model :detail, :person_id => :integer
400
- define_model :person do
401
- has_one :detail
402
- end
403
- Person.new.should_not @matcher.dependent(:destroy)
404
- end
405
-
406
- it "should accept an association with a valid :order option" do
407
- define_model :detail, :person_id => :integer
408
- define_model :person do
409
- has_one :detail, :order => :id
410
- end
411
- Person.new.should @matcher.order(:id)
412
- end
413
-
414
- it "should reject an association with a bad :order option" do
415
- define_model :detail, :person_id => :integer
416
- define_model :person do
417
- has_one :detail
418
- end
419
- Person.new.should_not @matcher.order(:id)
420
- end
421
-
422
- it "should accept an association with a valid :conditions option" do
423
- define_model :detail, :person_id => :integer, :disabled => :boolean
424
- define_model :person do
425
- has_one :detail, :conditions => { :disabled => true}
426
- end
427
- Person.new.should @matcher.conditions(:disabled => true)
428
- end
429
-
430
- it "should reject an association with a bad :conditions option" do
431
- define_model :detail, :person_id => :integer, :disabled => :boolean
432
- define_model :person do
433
- has_one :detail
434
- end
435
- Person.new.should_not @matcher.conditions(:disabled => true)
436
- end
437
-
438
- it "should accept an association with a valid :class_name option" do
439
- define_model :person_detail, :person_id => :integer, :disabled => :boolean
440
- define_model :person do
441
- has_one :detail, :class_name => 'PersonDetail'
442
- end
443
- Person.new.should @matcher.class_name('PersonDetail')
444
- end
445
-
446
- it "should reject an association with a bad :class_name option" do
447
- define_model :detail, :person_id => :integer, :disabled => :boolean
448
- define_model :person do
449
- has_one :detail
450
- end
451
- Person.new.should_not @matcher.class_name('PersonDetail')
452
- end
453
-
454
- it "should accept an association with a through" do
455
- define_model :detail
456
-
457
- define_model :account do
458
- has_one :detail
459
- end
460
-
461
- define_model :person do
462
- has_one :account
463
- has_one :detail, :through => :account
464
- end
465
-
466
- Person.new.should @matcher.through(:account)
467
- end
468
-
469
- it "should reject an association with a through" do
470
- define_model :detail
471
-
472
- define_model :person do
473
- has_one :detail
474
- end
475
-
476
- Person.new.should_not @matcher.through(:account)
477
- end
478
-
479
- context 'should accept an association with a false :validate option' do
480
- before do
481
- define_model :detail, :person_id => :integer, :disabled => :boolean
482
- define_model :person do
483
- has_one :detail, :validate => false
484
- end
485
- end
486
- subject { Person.new }
487
- specify { subject.should @matcher.validate(false) }
488
- specify { subject.should_not @matcher.validate(true) }
489
- specify { subject.should_not @matcher.validate }
490
- end
491
-
492
- context 'should accept an association with a true :validate option' do
493
- before do
494
- define_model :detail, :person_id => :integer, :disabled => :boolean
495
- define_model :person do
496
- has_one :detail, :validate => true
497
- end
498
- end
499
- subject { Person.new }
500
- specify { subject.should_not @matcher.validate(false) }
501
- specify { subject.should @matcher.validate(true) }
502
- specify { subject.should @matcher.validate }
503
- end
504
-
505
- context 'should accept an association without a :validate option' do
506
- before do
507
- define_model :detail, :person_id => :integer, :disabled => :boolean
508
- define_model :person do
509
- has_one :detail
510
- end
511
- end
512
- subject { Person.new }
513
- specify { subject.should @matcher.validate(false) }
514
- specify { subject.should_not @matcher.validate(true) }
515
- specify { subject.should_not @matcher.validate }
516
- end
517
-
518
- end
519
-
520
- context "have_and_belong_to_many" do
521
- before do
522
- @matcher = have_and_belong_to_many(:relatives)
523
- end
524
-
525
- it "should accept a valid association" do
526
- define_model :relatives
527
- define_model :person do
528
- has_and_belongs_to_many :relatives
529
- end
530
- define_model :people_relative, :person_id => :integer,
531
- :relative_id => :integer
532
- Person.new.should @matcher
533
- end
534
-
535
- it "should reject a nonexistent association" do
536
- define_model :relatives
537
- define_model :person
538
- define_model :people_relative, :person_id => :integer,
539
- :relative_id => :integer
540
- Person.new.should_not @matcher
541
- end
542
-
543
- it "should reject an association with a nonexistent join table" do
544
- define_model :relatives
545
- define_model :person do
546
- has_and_belongs_to_many :relatives
547
- end
548
- Person.new.should_not @matcher
549
- end
550
-
551
- it "should reject an association of the wrong type" do
552
- define_model :relatives, :person_id => :integer
553
- define_model :person do
554
- has_many :relatives
555
- end
556
- Person.new.should_not @matcher
557
- end
558
-
559
- it "should accept an association with a valid :conditions option" do
560
- define_model :relatives, :adopted => :boolean
561
- define_model :person do
562
- has_and_belongs_to_many :relatives, :conditions => { :adopted => true }
563
- end
564
- define_model :people_relative, :person_id => :integer,
565
- :relative_id => :integer
566
- Person.new.should @matcher.conditions(:adopted => true)
567
- end
568
-
569
- it "should reject an association with a bad :conditions option" do
570
- define_model :relatives, :adopted => :boolean
571
- define_model :person do
572
- has_and_belongs_to_many :relatives
573
- end
574
- define_model :people_relative, :person_id => :integer,
575
- :relative_id => :integer
576
- Person.new.should_not @matcher.conditions(:adopted => true)
577
- end
578
-
579
- it "should accept an association with a valid :class_name option" do
580
- define_model :person_relatives, :adopted => :boolean
581
- define_model :person do
582
- has_and_belongs_to_many :relatives, :class_name => 'PersonRelatives'
583
- end
584
- define_model :people_person_relative, :person_id => :integer,
585
- :person_relative_id => :integer
586
- Person.new.should @matcher.class_name('PersonRelatives')
587
- end
588
-
589
- it "should reject an association with a bad :class_name option" do
590
- define_model :relatives, :adopted => :boolean
591
- define_model :person do
592
- has_and_belongs_to_many :relatives
593
- end
594
- define_model :people_relative, :person_id => :integer,
595
- :relative_id => :integer
596
- Person.new.should_not @matcher.class_name('PersonRelatives')
597
- end
598
-
599
- context 'should accept an association with a false :validate option' do
600
- before do
601
- define_model :relatives, :adopted => :boolean
602
- define_model :person do
603
- has_and_belongs_to_many :relatives, :validate => false
604
- end
605
- define_model :people_relative, :person_id => :integer, :relative_id => :integer
606
- end
607
- subject { Person.new }
608
- specify { subject.should @matcher.validate(false) }
609
- specify { subject.should_not @matcher.validate(true) }
610
- specify { subject.should_not @matcher.validate }
611
- end
612
-
613
- context 'should accept an association with a true :validate option' do
614
- before do
615
- define_model :relatives, :adopted => :boolean
616
- define_model :person do
617
- has_and_belongs_to_many :relatives, :validate => true
618
- end
619
- define_model :people_relative, :person_id => :integer, :relative_id => :integer
620
- end
621
- subject { Person.new }
622
- specify { subject.should_not @matcher.validate(false) }
623
- specify { subject.should @matcher.validate(true) }
624
- specify { subject.should @matcher.validate }
625
- end
626
-
627
- context 'should accept an association without a :validate option' do
628
- before do
629
- define_model :relatives, :adopted => :boolean
630
- define_model :person do
631
- has_and_belongs_to_many :relatives
632
- end
633
- define_model :people_relative, :person_id => :integer, :relative_id => :integer
634
- end
635
- subject { Person.new }
636
- specify { subject.should @matcher.validate(false) }
637
- specify { subject.should_not @matcher.validate(true) }
638
- specify { subject.should_not @matcher.validate }
639
- end
640
-
641
- end
642
- end