rspec-expectations 2.99.2 → 3.0.0.beta1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (98) hide show
  1. checksums.yaml +14 -6
  2. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +2 -0
  4. data/Changelog.md +63 -104
  5. data/License.txt +1 -0
  6. data/README.md +14 -8
  7. data/features/README.md +1 -2
  8. data/features/built_in_matchers/README.md +3 -0
  9. data/features/built_in_matchers/be.feature +44 -44
  10. data/features/built_in_matchers/be_within.feature +1 -1
  11. data/features/built_in_matchers/comparisons.feature +97 -0
  12. data/features/built_in_matchers/cover.feature +3 -3
  13. data/features/built_in_matchers/end_with.feature +3 -3
  14. data/features/built_in_matchers/equality.feature +20 -23
  15. data/features/built_in_matchers/exist.feature +5 -5
  16. data/features/built_in_matchers/expect_error.feature +14 -14
  17. data/features/built_in_matchers/include.feature +15 -15
  18. data/features/built_in_matchers/match.feature +4 -5
  19. data/features/built_in_matchers/match_array.feature +37 -0
  20. data/features/built_in_matchers/predicates.feature +30 -6
  21. data/features/built_in_matchers/respond_to.feature +4 -4
  22. data/features/built_in_matchers/satisfy.feature +2 -2
  23. data/features/built_in_matchers/start_with.feature +3 -3
  24. data/features/built_in_matchers/types.feature +6 -6
  25. data/features/custom_matchers/access_running_example.feature +3 -3
  26. data/features/custom_matchers/define_matcher.feature +6 -34
  27. data/features/custom_matchers/define_matcher_outside_rspec.feature +2 -2
  28. data/features/custom_matchers/define_matcher_with_fluent_interface.feature +1 -1
  29. data/features/customized_message.feature +18 -1
  30. data/features/diffing.feature +3 -3
  31. data/features/implicit_docstrings.feature +9 -9
  32. data/features/step_definitions/additional_cli_steps.rb +0 -10
  33. data/features/support/env.rb +10 -3
  34. data/features/test_frameworks/test_unit.feature +0 -40
  35. data/lib/rspec-expectations.rb +0 -5
  36. data/lib/rspec/expectations.rb +4 -18
  37. data/lib/rspec/expectations/expectation_target.rb +10 -77
  38. data/lib/rspec/expectations/extensions.rb +0 -1
  39. data/lib/rspec/expectations/handler.rb +1 -5
  40. data/lib/rspec/expectations/syntax.rb +25 -5
  41. data/lib/rspec/expectations/version.rb +1 -1
  42. data/lib/rspec/matchers.rb +7 -102
  43. data/lib/rspec/matchers/built_in/base_matcher.rb +10 -17
  44. data/lib/rspec/matchers/built_in/be.rb +5 -18
  45. data/lib/rspec/matchers/built_in/be_within.rb +2 -8
  46. data/lib/rspec/matchers/built_in/change.rb +1 -39
  47. data/lib/rspec/matchers/built_in/has.rb +7 -40
  48. data/lib/rspec/matchers/built_in/include.rb +1 -1
  49. data/lib/rspec/matchers/built_in/match_array.rb +1 -1
  50. data/lib/rspec/matchers/built_in/raise_error.rb +44 -23
  51. data/lib/rspec/matchers/built_in/respond_to.rb +1 -7
  52. data/lib/rspec/matchers/built_in/satisfy.rb +1 -7
  53. data/lib/rspec/matchers/built_in/throw_symbol.rb +2 -10
  54. data/lib/rspec/matchers/built_in/yield.rb +4 -25
  55. data/lib/rspec/matchers/compatibility.rb +2 -2
  56. data/lib/rspec/{expectations → matchers}/configuration.rb +9 -6
  57. data/lib/rspec/matchers/dsl.rb +2 -4
  58. data/lib/rspec/matchers/matcher.rb +163 -283
  59. data/lib/rspec/matchers/operator_matcher.rb +57 -71
  60. data/lib/rspec/matchers/pretty.rb +0 -4
  61. data/lib/rspec/matchers/test_unit_integration.rb +5 -22
  62. data/spec/rspec/expectations/expectation_target_spec.rb +0 -62
  63. data/spec/rspec/expectations/extensions/kernel_spec.rb +0 -4
  64. data/spec/rspec/expectations_spec.rb +2 -43
  65. data/spec/rspec/matchers/base_matcher_spec.rb +12 -27
  66. data/spec/rspec/matchers/be_spec.rb +2 -71
  67. data/spec/rspec/matchers/change_spec.rb +1 -76
  68. data/spec/rspec/{expectations → matchers}/configuration_spec.rb +41 -21
  69. data/spec/rspec/matchers/description_generation_spec.rb +2 -21
  70. data/spec/rspec/matchers/equal_spec.rb +0 -26
  71. data/spec/rspec/matchers/has_spec.rb +0 -24
  72. data/spec/rspec/matchers/match_array_spec.rb +0 -13
  73. data/spec/rspec/matchers/matcher_spec.rb +325 -279
  74. data/spec/rspec/matchers/matchers_spec.rb +36 -0
  75. data/spec/rspec/matchers/operator_matcher_spec.rb +8 -27
  76. data/spec/rspec/matchers/raise_error_spec.rb +65 -209
  77. data/spec/rspec/matchers/yield_spec.rb +32 -9
  78. data/spec/spec_helper.rb +21 -6
  79. data/spec/support/classes.rb +7 -7
  80. data/spec/support/in_sub_process.rb +7 -8
  81. data/spec/support/shared_examples.rb +0 -42
  82. metadata +113 -84
  83. metadata.gz.sig +4 -0
  84. data/features/built_in_matchers/have.feature +0 -109
  85. data/features/built_in_matchers/operators.feature +0 -227
  86. data/lib/rspec/expectations/caller_filter.rb +0 -60
  87. data/lib/rspec/expectations/deprecation.rb +0 -27
  88. data/lib/rspec/expectations/extensions/array.rb +0 -9
  89. data/lib/rspec/matchers/be_close.rb +0 -12
  90. data/lib/rspec/matchers/built_in/have.rb +0 -273
  91. data/lib/rspec/matchers/differentiate_block_method_types.rb +0 -55
  92. data/lib/rspec/matchers/extensions/instance_eval_with_args.rb +0 -39
  93. data/lib/rspec/matchers/match_aliases.rb +0 -22
  94. data/spec/rspec/matchers/be_close_spec.rb +0 -25
  95. data/spec/rspec/matchers/differentiate_block_method_types_spec.rb +0 -39
  96. data/spec/rspec/matchers/have_spec.rb +0 -853
  97. data/spec/rspec/matchers/pretty_spec.rb +0 -23
  98. data/spec/support/helper_methods.rb +0 -42
@@ -1,853 +0,0 @@
1
- require 'spec_helper'
2
- require 'stringio'
3
-
4
- describe "have matcher" do
5
- let(:inflector) do
6
- Class.new do
7
- def self.pluralize(string)
8
- string.to_s + 's'
9
- end
10
- end
11
- end
12
-
13
- before(:each) do
14
- stub_const("ActiveSupport::Inflector", inflector)
15
- allow_deprecation
16
- end
17
-
18
- def expect_have_deprecation(expression, message)
19
- expect_deprecation_with_type(
20
- "`#{expression}`",
21
- message,
22
- 'the have matcher'
23
- )
24
- end
25
-
26
- def create_collection_owner_with(n)
27
- owner = RSpec::Expectations::Helper::CollectionOwner.new
28
- (1..n).each do |number|
29
- owner.add_to_collection_with_length_method(number)
30
- owner.add_to_collection_with_size_method(number)
31
- owner.add_to_collection_with_count_method(number)
32
- end
33
- owner
34
- end
35
-
36
- describe "expect(...).to have(n).items" do
37
- it_behaves_like "an RSpec matcher", :valid_value => [1, 2], :invalid_value => [1] do
38
- let(:matcher) { have(2).items }
39
- end
40
-
41
- it "passes if target has a collection of items with n members" do
42
- owner = create_collection_owner_with(3)
43
- expect(owner).to have(3).items_in_collection_with_length_method
44
- expect(owner).to have(3).items_in_collection_with_size_method
45
- expect(owner).to have(3).items_in_collection_with_count_method
46
- end
47
-
48
- it "converts :no to 0" do
49
- owner = create_collection_owner_with(0)
50
- expect(owner).to have(:no).items_in_collection_with_length_method
51
- expect(owner).to have(:no).items_in_collection_with_size_method
52
- expect(owner).to have(:no).items_in_collection_with_count_method
53
- end
54
-
55
- it "converts a String argument to Integer" do
56
- owner = create_collection_owner_with(3)
57
- expect(owner).to have('3').items_in_collection_with_length_method
58
- expect(owner).to have('3').items_in_collection_with_size_method
59
- expect(owner).to have('3').items_in_collection_with_count_method
60
- end
61
-
62
- it "fails if target has a collection of items with < n members" do
63
- owner = create_collection_owner_with(3)
64
- expect {
65
- expect(owner).to have(4).items_in_collection_with_length_method
66
- }.to fail_with("expected 4 items_in_collection_with_length_method, got 3")
67
- expect {
68
- expect(owner).to have(4).items_in_collection_with_size_method
69
- }.to fail_with("expected 4 items_in_collection_with_size_method, got 3")
70
- expect {
71
- expect(owner).to have(4).items_in_collection_with_count_method
72
- }.to fail_with("expected 4 items_in_collection_with_count_method, got 3")
73
- end
74
-
75
- it "fails if target has a collection of items with > n members" do
76
- owner = create_collection_owner_with(3)
77
- expect {
78
- expect(owner).to have(2).items_in_collection_with_length_method
79
- }.to fail_with("expected 2 items_in_collection_with_length_method, got 3")
80
- expect {
81
- expect(owner).to have(2).items_in_collection_with_size_method
82
- }.to fail_with("expected 2 items_in_collection_with_size_method, got 3")
83
- expect {
84
- expect(owner).to have(2).items_in_collection_with_count_method
85
- }.to fail_with("expected 2 items_in_collection_with_count_method, got 3")
86
- end
87
- end
88
-
89
- describe 'expect(...).to have(1).item when ActiveSupport::Inflector is defined' do
90
-
91
- it 'pluralizes the collection name' do
92
- owner = create_collection_owner_with(1)
93
- expect(owner).to have(1).item
94
- end
95
-
96
- context "when ActiveSupport::Inflector is partially loaded without its inflectors" do
97
-
98
- it "does not pluralize the collection name" do
99
- stub_const("ActiveSupport::Inflector", Module.new)
100
- owner = create_collection_owner_with(1)
101
- expect {
102
- expect(owner).to have(1).item
103
- }.to raise_error(NoMethodError)
104
- end
105
-
106
- end
107
- end
108
-
109
- describe 'expect(...).to have(1).item when Inflector is defined' do
110
- before { stub_const("Inflector", inflector) }
111
-
112
- it 'pluralizes the collection name' do
113
- owner = create_collection_owner_with(1)
114
- expect(owner).to have(1).item
115
- end
116
- end
117
-
118
- describe "expect(...).to have(n).items where result responds to items but returns something other than a collection" do
119
- it "provides a meaningful error" do
120
- owner = Class.new do
121
- def items
122
- Object.new
123
- end
124
- end.new
125
- expect do
126
- expect(owner).to have(3).items
127
- end.to raise_error("expected items to be a collection but it does not respond to #length, #size or #count")
128
- end
129
- end
130
-
131
- describe "expect(...).not_to have(n).items" do
132
-
133
- it "passes if target has a collection of items with < n members" do
134
- owner = create_collection_owner_with(3)
135
- expect(owner).not_to have(4).items_in_collection_with_length_method
136
- expect(owner).not_to have(4).items_in_collection_with_size_method
137
- expect(owner).not_to have(4).items_in_collection_with_count_method
138
- end
139
-
140
- it "passes if target has a collection of items with > n members" do
141
- owner = create_collection_owner_with(3)
142
- expect(owner).not_to have(2).items_in_collection_with_length_method
143
- expect(owner).not_to have(2).items_in_collection_with_size_method
144
- expect(owner).not_to have(2).items_in_collection_with_count_method
145
- end
146
-
147
- it "fails if target has a collection of items with n members" do
148
- owner = create_collection_owner_with(3)
149
- expect {
150
- expect(owner).not_to have(3).items_in_collection_with_length_method
151
- }.to fail_with("expected target not to have 3 items_in_collection_with_length_method, got 3")
152
- expect {
153
- expect(owner).not_to have(3).items_in_collection_with_size_method
154
- }.to fail_with("expected target not to have 3 items_in_collection_with_size_method, got 3")
155
- expect {
156
- expect(owner).not_to have(3).items_in_collection_with_count_method
157
- }.to fail_with("expected target not to have 3 items_in_collection_with_count_method, got 3")
158
- end
159
- end
160
-
161
- describe "expect(...).to have_exactly(n).items" do
162
-
163
- it "passes if target has a collection of items with n members" do
164
- owner = create_collection_owner_with(3)
165
- expect(owner).to have_exactly(3).items_in_collection_with_length_method
166
- expect(owner).to have_exactly(3).items_in_collection_with_size_method
167
- expect(owner).to have_exactly(3).items_in_collection_with_count_method
168
- end
169
-
170
- it "converts :no to 0" do
171
- owner = create_collection_owner_with(0)
172
- expect(owner).to have_exactly(:no).items_in_collection_with_length_method
173
- expect(owner).to have_exactly(:no).items_in_collection_with_size_method
174
- expect(owner).to have_exactly(:no).items_in_collection_with_count_method
175
- end
176
-
177
- it "fails if target has a collection of items with < n members" do
178
- owner = create_collection_owner_with(3)
179
- expect {
180
- expect(owner).to have_exactly(4).items_in_collection_with_length_method
181
- }.to fail_with("expected 4 items_in_collection_with_length_method, got 3")
182
- expect {
183
- expect(owner).to have_exactly(4).items_in_collection_with_size_method
184
- }.to fail_with("expected 4 items_in_collection_with_size_method, got 3")
185
- expect {
186
- expect(owner).to have_exactly(4).items_in_collection_with_count_method
187
- }.to fail_with("expected 4 items_in_collection_with_count_method, got 3")
188
- end
189
-
190
- it "fails if target has a collection of items with > n members" do
191
- owner = create_collection_owner_with(3)
192
- expect {
193
- expect(owner).to have_exactly(2).items_in_collection_with_length_method
194
- }.to fail_with("expected 2 items_in_collection_with_length_method, got 3")
195
- expect {
196
- expect(owner).to have_exactly(2).items_in_collection_with_size_method
197
- }.to fail_with("expected 2 items_in_collection_with_size_method, got 3")
198
- expect {
199
- expect(owner).to have_exactly(2).items_in_collection_with_count_method
200
- }.to fail_with("expected 2 items_in_collection_with_count_method, got 3")
201
- end
202
- end
203
-
204
- describe "expect(...).to have_at_least(n).items" do
205
-
206
- it "passes if target has a collection of items with n members" do
207
- owner = create_collection_owner_with(3)
208
- expect(owner).to have_at_least(3).items_in_collection_with_length_method
209
- expect(owner).to have_at_least(3).items_in_collection_with_size_method
210
- expect(owner).to have_at_least(3).items_in_collection_with_count_method
211
- end
212
-
213
- it "passes if target has a collection of items with > n members" do
214
- owner = create_collection_owner_with(3)
215
- expect(owner).to have_at_least(2).items_in_collection_with_length_method
216
- expect(owner).to have_at_least(2).items_in_collection_with_size_method
217
- expect(owner).to have_at_least(2).items_in_collection_with_count_method
218
- end
219
-
220
- it "fails if target has a collection of items with < n members" do
221
- owner = create_collection_owner_with(3)
222
- expect {
223
- expect(owner).to have_at_least(4).items_in_collection_with_length_method
224
- }.to fail_with("expected at least 4 items_in_collection_with_length_method, got 3")
225
- expect {
226
- expect(owner).to have_at_least(4).items_in_collection_with_size_method
227
- }.to fail_with("expected at least 4 items_in_collection_with_size_method, got 3")
228
- expect {
229
- expect(owner).to have_at_least(4).items_in_collection_with_count_method
230
- }.to fail_with("expected at least 4 items_in_collection_with_count_method, got 3")
231
- end
232
-
233
- it "provides educational negative failure messages" do
234
- #given
235
- owner = create_collection_owner_with(3)
236
- length_matcher = have_at_least(3).items_in_collection_with_length_method
237
- size_matcher = have_at_least(3).items_in_collection_with_size_method
238
- count_matcher = have_at_least(3).items_in_collection_with_count_method
239
-
240
- #when
241
- length_matcher.matches?(owner)
242
- size_matcher.matches?(owner)
243
- count_matcher.matches?(owner)
244
-
245
- #then
246
- expect(length_matcher.failure_message_for_should_not).to eq <<-EOF
247
- Isn't life confusing enough?
248
- Instead of having to figure out the meaning of this:
249
- expect(actual).not_to have_at_least(3).items_in_collection_with_length_method
250
- We recommend that you use this instead:
251
- expect(actual).to have_at_most(2).items_in_collection_with_length_method
252
- EOF
253
-
254
- expect(size_matcher.failure_message_for_should_not).to eq <<-EOF
255
- Isn't life confusing enough?
256
- Instead of having to figure out the meaning of this:
257
- expect(actual).not_to have_at_least(3).items_in_collection_with_size_method
258
- We recommend that you use this instead:
259
- expect(actual).to have_at_most(2).items_in_collection_with_size_method
260
- EOF
261
- expect(count_matcher.failure_message_for_should_not).to eq <<-EOF
262
- Isn't life confusing enough?
263
- Instead of having to figure out the meaning of this:
264
- expect(actual).not_to have_at_least(3).items_in_collection_with_count_method
265
- We recommend that you use this instead:
266
- expect(actual).to have_at_most(2).items_in_collection_with_count_method
267
- EOF
268
- end
269
- end
270
-
271
- describe "expect(...).to have_at_most(n).items" do
272
- it "passes if target has a collection of items with n members" do
273
- owner = create_collection_owner_with(3)
274
- expect(owner).to have_at_most(3).items_in_collection_with_length_method
275
- expect(owner).to have_at_most(3).items_in_collection_with_size_method
276
- expect(owner).to have_at_most(3).items_in_collection_with_count_method
277
- end
278
-
279
- it "fails if target has a collection of items with > n members" do
280
- owner = create_collection_owner_with(3)
281
- expect {
282
- expect(owner).to have_at_most(2).items_in_collection_with_length_method
283
- }.to fail_with("expected at most 2 items_in_collection_with_length_method, got 3")
284
- expect {
285
- expect(owner).to have_at_most(2).items_in_collection_with_size_method
286
- }.to fail_with("expected at most 2 items_in_collection_with_size_method, got 3")
287
- expect {
288
- expect(owner).to have_at_most(2).items_in_collection_with_count_method
289
- }.to fail_with("expected at most 2 items_in_collection_with_count_method, got 3")
290
- end
291
-
292
- it "passes if target has a collection of items with < n members" do
293
- owner = create_collection_owner_with(3)
294
- expect(owner).to have_at_most(4).items_in_collection_with_length_method
295
- expect(owner).to have_at_most(4).items_in_collection_with_size_method
296
- expect(owner).to have_at_most(4).items_in_collection_with_count_method
297
- end
298
-
299
- it "provides educational negative failure messages" do
300
- #given
301
- owner = create_collection_owner_with(3)
302
- length_matcher = have_at_most(3).items_in_collection_with_length_method
303
- size_matcher = have_at_most(3).items_in_collection_with_size_method
304
- count_matcher = have_at_most(3).items_in_collection_with_count_method
305
-
306
- #when
307
- length_matcher.matches?(owner)
308
- size_matcher.matches?(owner)
309
- count_matcher.matches?(owner)
310
-
311
- #then
312
- expect(length_matcher.failure_message_for_should_not).to eq <<-EOF
313
- Isn't life confusing enough?
314
- Instead of having to figure out the meaning of this:
315
- expect(actual).not_to have_at_most(3).items_in_collection_with_length_method
316
- We recommend that you use this instead:
317
- expect(actual).to have_at_least(4).items_in_collection_with_length_method
318
- EOF
319
-
320
- expect(size_matcher.failure_message_for_should_not).to eq <<-EOF
321
- Isn't life confusing enough?
322
- Instead of having to figure out the meaning of this:
323
- expect(actual).not_to have_at_most(3).items_in_collection_with_size_method
324
- We recommend that you use this instead:
325
- expect(actual).to have_at_least(4).items_in_collection_with_size_method
326
- EOF
327
-
328
- expect(count_matcher.failure_message_for_should_not).to eq <<-EOF
329
- Isn't life confusing enough?
330
- Instead of having to figure out the meaning of this:
331
- expect(actual).not_to have_at_most(3).items_in_collection_with_count_method
332
- We recommend that you use this instead:
333
- expect(actual).to have_at_least(4).items_in_collection_with_count_method
334
- EOF
335
- end
336
- end
337
-
338
- describe "have(n).items(args, block)" do
339
- it "passes args to target" do
340
- target = double("target")
341
- target.should_receive(:items).with("arg1","arg2").and_return([1,2,3])
342
- expect(target).to have(3).items("arg1","arg2")
343
- end
344
-
345
- it "passes block to target" do
346
- target = double("target")
347
- block = lambda { 5 }
348
- target.should_receive(:items).with("arg1","arg2", block).and_return([1,2,3])
349
- expect(target).to have(3).items("arg1","arg2", block)
350
- end
351
- end
352
-
353
- describe "have(n).items where target IS a collection" do
354
- it "references the number of items IN the collection" do
355
- expect([1,2,3]).to have(3).items
356
- end
357
-
358
- it "fails when the number of items IN the collection is not as expected" do
359
- expect {
360
- expect([1,2,3]).to have(7).items
361
- }.to fail_with("expected 7 items, got 3")
362
- end
363
- end
364
-
365
- describe "have(n).characters where target IS a String" do
366
- it "passes if the length is correct" do
367
- expect("this string").to have(11).characters
368
- end
369
-
370
- it "fails if the length is incorrect" do
371
- expect {
372
- expect("this string").to have(12).characters
373
- }.to fail_with("expected 12 characters, got 11")
374
- end
375
- end
376
-
377
- describe "have(n).things on an object which is not a collection nor contains one" do
378
- it "fails" do
379
- expect {
380
- expect(Object.new).to have(2).things
381
- }.to raise_error(NoMethodError) { |e|
382
- expect(e.name).to eq :things
383
- }
384
- end
385
- end
386
-
387
- describe RSpec::Matchers::BuiltIn::Have, "for a collection owner that implements #send" do
388
- before(:each) do
389
- @collection = Object.new
390
- def @collection.floozles; [1,2] end
391
- def @collection.send; :sent; end
392
- end
393
-
394
- it "works in the straightforward case" do
395
- expect(@collection).to have(2).floozles
396
- end
397
-
398
- it "works when doing automatic pluralization" do
399
- expect(@collection).to have_at_least(1).floozle
400
- end
401
-
402
- it "blows up when the owner doesn't respond to that method" do
403
- expect {
404
- expect(@collection).to have(99).problems
405
- }.to raise_error(NoMethodError, /problems/)
406
- end
407
-
408
- it 'works when #send is defined directly on an array' do
409
- array = [1, 2]
410
- def array.send; :sent; end
411
-
412
- expect(array).to have(2).items
413
- end
414
- end
415
-
416
- if RUBY_VERSION >= '2.0'
417
- describe RSpec::Matchers::BuiltIn::Have, "for an Enumerator whose size is nil but count is supplied" do
418
- let(:enumerator) { %w[a b c d].to_enum(:each) }
419
-
420
- it 'works fine' do
421
- expect(enumerator).to have(4).items
422
- end
423
- end
424
- end
425
-
426
- describe RSpec::Matchers::BuiltIn::Have do
427
- it "has method_missing as private" do
428
- expect(described_class.private_instance_methods).to include_method(:method_missing)
429
- end
430
-
431
- it "does not respond_to? method_missing (because it's private)" do
432
- formatter = described_class.new(0, StringIO.new)
433
- expect(formatter).not_to respond_to(:method_missing)
434
- end
435
-
436
- describe "respond_to?" do
437
- before :each do
438
- @have = described_class.new(:foo)
439
- @a_method_which_have_defines = described_class.instance_methods.first
440
- @a_method_which_object_defines = Object.instance_methods.first
441
- end
442
-
443
- it "is true for a method which Have defines" do
444
- expect(@have).to respond_to(@a_method_which_have_defines)
445
- end
446
-
447
- it "is true for a method that it's superclass (Object) defines" do
448
- expect(@have).to respond_to(@a_method_which_object_defines)
449
- end
450
-
451
- it "is false for a method which neither Object nor nor Have defines" do
452
- expect(@have).not_to respond_to(:foo_bar_baz)
453
- end
454
-
455
- it "is false if the owner doesn't respond to the method" do
456
- have = described_class.new(99)
457
- expect(have).not_to respond_to(:problems)
458
- end
459
-
460
- it "is true if the owner responds to the method" do
461
- have = described_class.new(:a_symbol)
462
- expect(have).to respond_to(:to_sym)
463
- end
464
- end
465
- end
466
-
467
- context "deprecations for the have matcher" do
468
- it "has the correct call site in the deprecation message" do
469
- expect_deprecation_with_call_site(__FILE__, __LINE__ + 1)
470
- expect([1, 2, 3]).to have(3).items
471
- end
472
-
473
- context "when the target is a collection" do
474
- it "prints a specific message for the positive expectation format" do
475
- expectation_expression = "expect(collection).to have(3).items"
476
-
477
- message = "the rspec-collection_matchers gem " +
478
- "or replace your expectation with something like " +
479
- "`expect(collection.size).to eq(3)`"
480
-
481
- expect_have_deprecation(expectation_expression, message)
482
-
483
- expect([1, 2, 3]).to have(3).items
484
- end
485
-
486
- it "prints a specific message for the negative expectation format" do
487
- expectation_expression = "expect(collection).not_to have(4).items"
488
-
489
- message = "the rspec-collection_matchers gem " +
490
- "or replace your expectation with something like " +
491
- "`expect(collection.size).to_not eq(4)`"
492
-
493
- expect_have_deprecation(expectation_expression, message)
494
-
495
- expect([1, 2, 3]).to_not have(4).items
496
- end
497
- end
498
-
499
- context "when the target owns a collection" do
500
- class self::BagOfWords
501
- attr_reader :words
502
-
503
- def initialize(words)
504
- @words = words
505
- end
506
- end
507
-
508
- it "prints a specific message for the positive expectation format" do
509
- expectation_expression = "expect(collection_owner).to have(3).words"
510
-
511
- message = "the rspec-collection_matchers gem " +
512
- "or replace your expectation with something like " +
513
- "`expect(collection_owner.words.size).to eq(3)`"
514
-
515
- expect_have_deprecation(expectation_expression, message)
516
-
517
- target = BagOfWords.new(%w[foo bar baz])
518
- expect(target).to have(3).words
519
- end
520
-
521
- it "prints a specific message for the negative expectation format" do
522
- expectation_expression = "expect(collection_owner).not_to have(4).words"
523
-
524
- message = "the rspec-collection_matchers gem " +
525
- "or replace your expectation with something like " +
526
- "`expect(collection_owner.words.size).to_not eq(4)`"
527
-
528
- expect_have_deprecation(expectation_expression, message)
529
-
530
- target = BagOfWords.new(%w[foo bar baz])
531
- expect(target).to_not have(4).words
532
- end
533
- end
534
-
535
- context "when the target is a Rails record" do
536
- class TheModel
537
- attr_reader :errors
538
-
539
- def initialize(errors)
540
- @errors = {}
541
- @errors[:attr] = Array(errors)
542
- end
543
-
544
- def errors_on(attr, _ignore_opts = {})
545
- Array(@errors[attr]).flatten.compact
546
- end
547
- alias_method :error_on, :errors_on
548
- end
549
-
550
- let(:message_preamble) do
551
- "the rspec-collection_matchers gem " +
552
- "or replace your expectation with something like "
553
- end
554
-
555
- before do
556
- stub_const "RSpec::Rails", Module.new
557
- end
558
-
559
- it "prints a specific message for the positive expectation format" do
560
- expectation_expression = "expect(record).to have(2).errors_on(:attr)"
561
- message = message_preamble + <<-EOS.gsub(/^\s+\|/, '')
562
- |
563
- |
564
- | record.valid?
565
- | expect(record.errors[:attr].size).to eq(2)
566
- |
567
- EOS
568
-
569
- expect_have_deprecation(expectation_expression, message)
570
-
571
- target = TheModel.new(%w(foo bar))
572
- expect(target).to have(2).errors_on(:attr)
573
- end
574
-
575
- it "prints a specific message for the negative expectation format" do
576
- expectation_expression = "expect(record).not_to have(2).errors_on(:attr)"
577
- message = message_preamble + <<-EOS.gsub(/^\s+\|/, '')
578
- |
579
- |
580
- | record.valid?
581
- | expect(record.errors[:attr].size).to_not eq(2)
582
- |
583
- EOS
584
-
585
- expect_have_deprecation(expectation_expression, message)
586
-
587
- target = TheModel.new('foo')
588
- expect(target).not_to have(2).errors_on(:attr)
589
- end
590
-
591
- it "prints message for singular form: `error_on`" do
592
- expectation_expression = "expect(record).to have(1).error_on(:attr)"
593
- message = message_preamble + <<-EOS.gsub(/^\s+\|/, '')
594
- |
595
- |
596
- | record.valid?
597
- | expect(record.errors[:attr].size).to eq(1)
598
- |
599
- EOS
600
-
601
- expect_have_deprecation(expectation_expression, message)
602
-
603
- target = TheModel.new('foo')
604
- expect(target).to have(1).error_on(:attr)
605
- end
606
-
607
- it "includes a validation context when provided" do
608
- expectation_expression = "expect(record).to have(2).errors_on(:attr, :context => :spec)"
609
- message = message_preamble + <<-EOS.gsub(/^\s+\|/, '')
610
- |
611
- |
612
- | record.valid?(:spec)
613
- | expect(record.errors[:attr].size).to eq(2)
614
- |
615
- EOS
616
-
617
- expect_have_deprecation(expectation_expression, message)
618
-
619
- target = TheModel.new(%w(foo bar))
620
- options = {:context => :spec, :should_be_ignored => true}
621
- expect(target).to have(2).errors_on(:attr, options)
622
- end
623
- end
624
-
625
- context "when the target is an enumerator" do
626
- it "prints a specific message for the positive expectation format" do
627
- target = %w[a b c].to_enum(:each)
628
-
629
- expectation_expression = "expect(collection).to have(3).letters"
630
-
631
- message = "the rspec-collection_matchers gem " +
632
- "or replace your expectation with something like " +
633
- "`expect(collection.count).to eq(3)`"
634
-
635
- expect_have_deprecation(expectation_expression, message)
636
-
637
- expect(target).to have(3).letters
638
- end
639
-
640
- it "prints a specific message for the negative expectation format" do
641
- target = %w[a b c].to_enum(:each)
642
-
643
- expectation_expression = "expect(collection).not_to have(4).letters"
644
-
645
- message = "the rspec-collection_matchers gem " +
646
- "or replace your expectation with something like " +
647
- "`expect(collection.count).to_not eq(4)`"
648
-
649
- expect_have_deprecation(expectation_expression, message)
650
-
651
- expect(target).to_not have(4).letters
652
- end
653
- end
654
- end
655
-
656
- context "deprecations for the have_at_most matcher" do
657
- it "has the correct call site in the deprecation message" do
658
- expect_deprecation_with_call_site(__FILE__, __LINE__ + 1)
659
- expect([1, 2, 3]).to have_at_most(3).items
660
- end
661
-
662
- context "when the target is a collection" do
663
- it "prints a specific message for the positive expectation format" do
664
- expectation_expression = "expect(collection).to have_at_most(3).items"
665
-
666
- message = "the rspec-collection_matchers gem " +
667
- "or replace your expectation with something like " +
668
- "`expect(collection.size).to be <= 3`"
669
-
670
- expect_have_deprecation(expectation_expression, message)
671
-
672
- expect([1, 2, 3]).to have_at_most(3).items
673
- end
674
-
675
- it "prints a specific message for the negative expectation format" do
676
- expectation_expression = "expect(collection).not_to have_at_most(2).items"
677
-
678
- message = "the rspec-collection_matchers gem " +
679
- "or replace your expectation with something like " +
680
- "`expect(collection.size).to be > 2`"
681
-
682
- expect_have_deprecation(expectation_expression, message)
683
-
684
- expect([1, 2, 3]).to_not have_at_most(2).items
685
- end
686
- end
687
-
688
- context "when the target owns a collection" do
689
- class self::BagOfWords
690
- attr_reader :words
691
-
692
- def initialize(words)
693
- @words = words
694
- end
695
- end
696
-
697
- it "prints a specific message for the positive expectation format" do
698
- expectation_expression = "expect(collection_owner).to have_at_most(3).words"
699
-
700
- message = "the rspec-collection_matchers gem " +
701
- "or replace your expectation with something like " +
702
- "`expect(collection_owner.words.size).to be <= 3`"
703
-
704
- expect_have_deprecation(expectation_expression, message)
705
-
706
- target = BagOfWords.new(%w[foo bar baz])
707
- expect(target).to have_at_most(3).words
708
- end
709
-
710
- it "prints a specific message for the negative expectation format" do
711
- expectation_expression = "expect(collection_owner).not_to have_at_most(2).words"
712
-
713
- message = "the rspec-collection_matchers gem " +
714
- "or replace your expectation with something like " +
715
- "`expect(collection_owner.words.size).to be > 2`"
716
-
717
- expect_have_deprecation(expectation_expression, message)
718
-
719
- target = BagOfWords.new(%w[foo bar baz])
720
- expect(target).to_not have_at_most(2).words
721
- end
722
- end
723
-
724
- context "when the target is an enumerator" do
725
- it "prints a specific message for the positive expectation format" do
726
- target = %w[a b c].to_enum(:each)
727
-
728
- expectation_expression = "expect(collection).to have_at_most(3).letters"
729
-
730
- message = "the rspec-collection_matchers gem " +
731
- "or replace your expectation with something like " +
732
- "`expect(collection.count).to be <= 3`"
733
-
734
- expect_have_deprecation(expectation_expression, message)
735
-
736
- expect(target).to have_at_most(3).letters
737
- end
738
-
739
- it "prints a specific message for the negative expectation format" do
740
- target = %w[a b c].to_enum(:each)
741
-
742
- expectation_expression = "expect(collection).not_to have_at_most(2).letters"
743
-
744
- message = "the rspec-collection_matchers gem " +
745
- "or replace your expectation with something like " +
746
- "`expect(collection.count).to be > 2`"
747
-
748
- expect_have_deprecation(expectation_expression, message)
749
-
750
- expect(target).to_not have_at_most(2).letters
751
- end
752
- end
753
- end
754
-
755
- context "deprecations for the have_at_least matcher" do
756
- it "has the correct call site in the deprecation message" do
757
- expect_deprecation_with_call_site(__FILE__, __LINE__ + 1)
758
- expect([1, 2, 3]).to have_at_least(3).items
759
- end
760
-
761
- context "when the target is a collection" do
762
- it "prints a specific message for the positive expectation format" do
763
- expectation_expression = "expect(collection).to have_at_least(3).items"
764
-
765
- message = "the rspec-collection_matchers gem " +
766
- "or replace your expectation with something like " +
767
- "`expect(collection.size).to be >= 3`"
768
-
769
- expect_have_deprecation(expectation_expression, message)
770
-
771
- expect([1, 2, 3]).to have_at_least(3).items
772
- end
773
-
774
- it "prints a specific message for the negative expectation format" do
775
- expectation_expression = "expect(collection).not_to have_at_least(4).items"
776
-
777
- message = "the rspec-collection_matchers gem " +
778
- "or replace your expectation with something like " +
779
- "`expect(collection.size).to be < 4`"
780
-
781
- expect_have_deprecation(expectation_expression, message)
782
-
783
- expect([1, 2, 3]).to_not have_at_least(4).items
784
- end
785
- end
786
-
787
- context "when the target owns a collection" do
788
- class BagOfWords
789
- attr_reader :words
790
-
791
- def initialize(words)
792
- @words = words
793
- end
794
- end
795
-
796
- it "prints a specific message for the positive expectation format" do
797
- expectation_expression = "expect(collection_owner).to have_at_least(3).words"
798
-
799
- message = "the rspec-collection_matchers gem " +
800
- "or replace your expectation with something like " +
801
- "`expect(collection_owner.words.size).to be >= 3`"
802
-
803
- expect_have_deprecation(expectation_expression, message)
804
-
805
- target = BagOfWords.new(%w[foo bar baz])
806
- expect(target).to have_at_least(3).words
807
- end
808
-
809
- it "prints a specific message for the negative expectation format" do
810
- expectation_expression = "expect(collection_owner).not_to have_at_least(4).words"
811
-
812
- message = "the rspec-collection_matchers gem " +
813
- "or replace your expectation with something like " +
814
- "`expect(collection_owner.words.size).to be < 4`"
815
-
816
- expect_have_deprecation(expectation_expression, message)
817
-
818
- target = BagOfWords.new(%w[foo bar baz])
819
- expect(target).to_not have_at_least(4).words
820
- end
821
- end
822
-
823
- context "when the target is an enumerator" do
824
- it "prints a specific message for the positive expectation format" do
825
- target = %w[a b c].to_enum(:each)
826
-
827
- expectation_expression = "expect(collection).to have_at_least(3).letters"
828
-
829
- message = "the rspec-collection_matchers gem " +
830
- "or replace your expectation with something like " +
831
- "`expect(collection.count).to be >= 3`"
832
-
833
- expect_have_deprecation(expectation_expression, message)
834
-
835
- expect(target).to have_at_least(3).letters
836
- end
837
-
838
- it "prints a specific message for the negative expectation format" do
839
- target = %w[a b c].to_enum(:each)
840
-
841
- expectation_expression = "expect(collection).not_to have_at_least(4).letters"
842
-
843
- message = "the rspec-collection_matchers gem " +
844
- "or replace your expectation with something like " +
845
- "`expect(collection.count).to be < 4`"
846
-
847
- expect_have_deprecation(expectation_expression, message)
848
-
849
- expect(target).to_not have_at_least(4).letters
850
- end
851
- end
852
- end
853
- end