rspec-mocks 2.14.6 → 2.99.0.beta1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. checksums.yaml +8 -8
  2. data/Changelog.md +20 -14
  3. data/lib/rspec/mocks.rb +6 -2
  4. data/lib/rspec/mocks/any_instance/chain.rb +28 -0
  5. data/lib/rspec/mocks/any_instance/expectation_chain.rb +5 -10
  6. data/lib/rspec/mocks/any_instance/recorder.rb +4 -1
  7. data/lib/rspec/mocks/any_instance/stub_chain.rb +5 -5
  8. data/lib/rspec/mocks/caller_filter.rb +55 -0
  9. data/lib/rspec/mocks/configuration.rb +19 -0
  10. data/lib/rspec/mocks/deprecation.rb +9 -1
  11. data/lib/rspec/mocks/extensions/proc.rb +63 -0
  12. data/lib/rspec/mocks/framework.rb +2 -0
  13. data/lib/rspec/mocks/matchers/receive.rb +2 -9
  14. data/lib/rspec/mocks/message_expectation.rb +92 -22
  15. data/lib/rspec/mocks/method_double.rb +2 -2
  16. data/lib/rspec/mocks/proxy_for_nil.rb +2 -2
  17. data/lib/rspec/mocks/space.rb +4 -5
  18. data/lib/rspec/mocks/stub_chain.rb +1 -1
  19. data/lib/rspec/mocks/syntax.rb +3 -3
  20. data/lib/rspec/mocks/targets.rb +1 -1
  21. data/lib/rspec/mocks/test_double.rb +8 -2
  22. data/lib/rspec/mocks/version.rb +1 -1
  23. data/spec/rspec/mocks/and_yield_spec.rb +1 -1
  24. data/spec/rspec/mocks/any_instance/message_chains_spec.rb +3 -3
  25. data/spec/rspec/mocks/any_instance_spec.rb +233 -84
  26. data/spec/rspec/mocks/argument_expectation_spec.rb +4 -4
  27. data/spec/rspec/mocks/block_return_value_spec.rb +49 -11
  28. data/spec/rspec/mocks/bug_report_10263_spec.rb +1 -1
  29. data/spec/rspec/mocks/bug_report_8165_spec.rb +2 -2
  30. data/spec/rspec/mocks/combining_implementation_instructions_spec.rb +4 -4
  31. data/spec/rspec/mocks/double_spec.rb +7 -0
  32. data/spec/rspec/mocks/failing_argument_matchers_spec.rb +1 -0
  33. data/spec/rspec/mocks/matchers/receive_spec.rb +10 -8
  34. data/spec/rspec/mocks/mock_space_spec.rb +10 -0
  35. data/spec/rspec/mocks/mock_spec.rb +20 -1
  36. data/spec/rspec/mocks/mutate_const_spec.rb +25 -25
  37. data/spec/rspec/mocks/null_object_mock_spec.rb +7 -0
  38. data/spec/rspec/mocks/passing_argument_matchers_spec.rb +4 -2
  39. data/spec/rspec/mocks/record_messages_spec.rb +4 -4
  40. data/spec/rspec/mocks/space_spec.rb +1 -1
  41. data/spec/rspec/mocks/stub_chain_spec.rb +8 -12
  42. data/spec/rspec/mocks/syntax_agnostic_message_matchers_spec.rb +20 -0
  43. data/spec/rspec/mocks/test_double_spec.rb +0 -5
  44. data/spec/rspec/mocks_spec.rb +14 -0
  45. data/spec/spec_helper.rb +27 -0
  46. metadata +7 -5
@@ -9,23 +9,23 @@ module RSpec
9
9
  it "considers an object that responds to #matches? and #failure_message_for_should to be a matcher" do
10
10
  obj.stub(:matches?)
11
11
  obj.stub(:failure_message_for_should)
12
- expect(argument_expectation.send(:is_matcher?, obj)).to be_true
12
+ expect(argument_expectation.send(:is_matcher?, obj)).to be true
13
13
  end
14
14
 
15
15
  it "considers an object that responds to #matches? and #failure_message to be a matcher for backward compatibility" do
16
16
  obj.stub(:matches?)
17
17
  obj.stub(:failure_message)
18
- expect(argument_expectation.send(:is_matcher?, obj)).to be_true
18
+ expect(argument_expectation.send(:is_matcher?, obj)).to be true
19
19
  end
20
20
 
21
21
  it "does NOT consider an object that only responds to #matches? to be a matcher" do
22
22
  obj.stub(:matches?)
23
- expect(argument_expectation.send(:is_matcher?, obj)).to be_false
23
+ expect(argument_expectation.send(:is_matcher?, obj)).to be false
24
24
  end
25
25
 
26
26
  it "does not consider a null object to be a matcher" do
27
27
  obj.as_null_object
28
- expect(argument_expectation.send(:is_matcher?, obj)).to be_false
28
+ expect(argument_expectation.send(:is_matcher?, obj)).to be false
29
29
  end
30
30
  end
31
31
  end
@@ -1,17 +1,6 @@
1
1
  require "spec_helper"
2
2
 
3
3
  describe "a double declaration with a block handed to:" do
4
- # The "receives a block" part is important: 1.8.7 has a bug that reports the
5
- # wrong arity when a block receives a block.
6
- it 'forwards all given args to the block, even when it receives a block', :unless => RUBY_VERSION.to_s == '1.8.6' do
7
- obj = Object.new
8
- yielded_args = []
9
- eval("obj.stub(:foo) { |*args, &bl| yielded_args << args }")
10
- obj.foo(1, 2, 3)
11
-
12
- expect(yielded_args).to eq([[1, 2, 3]])
13
- end
14
-
15
4
  describe "should_receive" do
16
5
  it "returns the value of executing the block" do
17
6
  obj = Object.new
@@ -36,6 +25,17 @@ describe "a double declaration with a block handed to:" do
36
25
  end
37
26
 
38
27
  it "does not complain if a lambda block and mismatched arguments are passed" do
28
+ RSpec.stub :deprecate
29
+ obj = Object.new
30
+ obj.stub(:foo, &lambda { 'bar' })
31
+ expect(obj.foo(1, 2)).to eq('bar')
32
+ end
33
+
34
+ it 'warns of deprection if argument counts dont match' do
35
+ expect(RSpec).to receive(:deprecate) do |message, opts|
36
+ expect(message).to eq "stubbing implementations with mismatched arity"
37
+ expect(opts[:call_site]).to match %r%/spec/rspec/mocks/block_return_value_spec.rb%
38
+ end
39
39
  obj = Object.new
40
40
  obj.stub(:foo, &lambda { 'bar' })
41
41
  expect(obj.foo(1, 2)).to eq('bar')
@@ -50,10 +50,37 @@ describe "a double declaration with a block handed to:" do
50
50
  end
51
51
 
52
52
  it "does not complain if a lambda block and mismatched arguments are passed" do
53
+ RSpec.stub :deprecate
54
+ obj = Object.new
55
+ obj.stub(:foo).with(1, 2, &lambda { 'bar' })
56
+ expect(obj.foo(1, 2)).to eq('bar')
57
+ end
58
+
59
+ it 'warns of deprection if argument counts dont match' do
60
+ expect(RSpec).to receive(:deprecate) do |message, opts|
61
+ expect(message).to eq "stubbing implementations with mismatched arity"
62
+ expect(opts[:call_site]).to match %r%/spec/rspec/mocks/block_return_value_spec.rb%
63
+ end
53
64
  obj = Object.new
54
65
  obj.stub(:foo).with(1, 2, &lambda { 'bar' })
55
66
  expect(obj.foo(1, 2)).to eq('bar')
56
67
  end
68
+
69
+ it 'warns of deprecation when provided block but no arguments' do
70
+ expect(RSpec).to receive(:deprecate) do |message, opts|
71
+ expect(message).to match(/Using the return value of a `with` block/)
72
+ end
73
+ obj = Object.new
74
+ obj.stub(:foo).with {|x| 'baz' }.and_return('bar')
75
+ expect(obj.foo(1)).to eq('bar')
76
+ end
77
+
78
+ it 'includes callsite in deprecation of provided block but no arguments' do
79
+ obj = Object.new
80
+ expect_deprecation_with_call_site __FILE__, __LINE__ + 1
81
+ obj.stub(:foo).with {|x| 'baz' }.and_return('bar')
82
+ expect(obj.foo(1)).to eq('bar')
83
+ end
57
84
  end
58
85
 
59
86
  %w[once twice ordered and_return].each do |method|
@@ -65,6 +92,17 @@ describe "a double declaration with a block handed to:" do
65
92
  end
66
93
 
67
94
  it "does not complain if a lambda block and mismatched arguments are passed" do
95
+ RSpec.stub :deprecate
96
+ obj = Object.new
97
+ obj.stub(:foo).send(method, &lambda { 'bar' })
98
+ expect(obj.foo(1, 2)).to eq('bar')
99
+ end
100
+
101
+ it 'warns of deprection if argument counts dont match' do
102
+ expect(RSpec).to receive(:deprecate) do |message, opts|
103
+ expect(message).to eq "stubbing implementations with mismatched arity"
104
+ expect(opts[:call_site]).to match %r%/spec/rspec/mocks/block_return_value_spec.rb%
105
+ end
68
106
  obj = Object.new
69
107
  obj.stub(:foo).send(method, &lambda { 'bar' })
70
108
  expect(obj.foo(1, 2)).to eq('bar')
@@ -5,7 +5,7 @@ describe "Double" do
5
5
 
6
6
  specify "when one example has an expectation inside the block passed to should_receive" do
7
7
  test_double.should_receive(:msg) do |arg|
8
- expect(arg).to be_true #this call exposes the problem
8
+ expect(arg).to be true #this call exposes the problem
9
9
  end
10
10
  begin
11
11
  test_double.msg(false)
@@ -16,7 +16,7 @@ describe "An object where respond_to? is true and does not have method" do
16
16
  obj = Object.new
17
17
  obj.should_receive(:respond_to?).with(:foobar).and_return(true)
18
18
  obj.should_receive(:foobar).and_return(:baz)
19
- expect(obj.respond_to?(:foobar)).to be_true
19
+ expect(obj.respond_to?(:foobar)).to be true
20
20
  expect(obj.foobar).to eq :baz
21
21
  end
22
22
 
@@ -24,7 +24,7 @@ describe "An object where respond_to? is true and does not have method" do
24
24
  obj = double("obj")
25
25
  obj.should_receive(:respond_to?).with(:foobar).and_return(true)
26
26
  obj.should_receive(:foobar).and_return(:baz)
27
- expect(obj.respond_to?(:foobar)).to be_true
27
+ expect(obj.respond_to?(:foobar)).to be true
28
28
  expect(obj.foobar).to eq :baz
29
29
  end
30
30
 
@@ -21,7 +21,7 @@ module RSpec
21
21
  expect(dbl.foo(:arg, &b)).to eq(3)
22
22
  }.to yield_with_args(5)
23
23
 
24
- expect(@block_called).to be_true
24
+ expect(@block_called).to be true
25
25
  end
26
26
 
27
27
  it 'works when passing a block to `stub`' do
@@ -60,7 +60,7 @@ module RSpec
60
60
  end
61
61
  end
62
62
 
63
- it 'works when passing a block to `any_number_of_times`' do
63
+ it 'works when passing a block to `any_number_of_times`', :silence_warnings do
64
64
  verify_combined_implementation do |dbl|
65
65
  dbl.should_receive(:foo).any_number_of_times { @block_called = true }
66
66
  end
@@ -117,7 +117,7 @@ module RSpec
117
117
  expect { dbl.foo(&b) }.to raise_error("boom")
118
118
  }.to yield_with_args(5)
119
119
 
120
- expect(block_called).to be_true
120
+ expect(block_called).to be true
121
121
  end
122
122
 
123
123
  it 'can combine and_yield and and_throw' do
@@ -138,7 +138,7 @@ module RSpec
138
138
  expect { dbl.foo(&b) }.to throw_symbol(:bar)
139
139
  }.to yield_with_args(5)
140
140
 
141
- expect(block_called).to be_true
141
+ expect(block_called).to be true
142
142
  end
143
143
 
144
144
  it 'returns `nil` from all terminal actions to discourage further configuration' do
@@ -21,4 +21,11 @@ describe "double" do
21
21
  mock("TestDouble")
22
22
  end
23
23
  end
24
+
25
+ it 'does not get string vs symbol messages confused' do
26
+ dbl = double("foo" => 1)
27
+ allow(dbl).to receive(:foo).and_return(2)
28
+ expect(dbl.foo).to eq(2)
29
+ expect { reset dbl }.not_to raise_error
30
+ end
24
31
  end
@@ -84,6 +84,7 @@ module RSpec
84
84
  end
85
85
 
86
86
  it "fails with block matchers" do
87
+ allow_deprecation
87
88
  expect do
88
89
  @double.should_receive(:msg).with {|arg| expect(arg).to eq :received }
89
90
  @double.msg :no_msg_for_you
@@ -9,10 +9,6 @@ module RSpec
9
9
  ::RSpec::Mocks.space.verify_all
10
10
  end
11
11
 
12
- def reset_all
13
- ::RSpec::Mocks.space.reset_all
14
- end
15
-
16
12
  shared_examples_for "a receive matcher" do |*options|
17
13
  it 'allows the caller to configure how the subject responds' do
18
14
  wrapped.to receive(:foo).and_return(5)
@@ -63,6 +59,14 @@ module RSpec
63
59
  wrapped.to eq(3)
64
60
  }.to raise_error(UnsupportedMatcherError)
65
61
  end
62
+
63
+ it 'does not get confused by messages being passed as strings and symbols' do
64
+ wrapped.to receive(:foo).with(1) { :a }
65
+ wrapped.to receive("foo").with(2) { :b }
66
+
67
+ expect(receiver.foo(1)).to eq(:a)
68
+ expect(receiver.foo(2)).to eq(:b)
69
+ end
66
70
  end
67
71
 
68
72
  shared_examples_for "an expect syntax allowance" do |*options|
@@ -101,8 +105,6 @@ module RSpec
101
105
  expect {
102
106
  verify_all
103
107
  }.to raise_error(RSpec::Mocks::MockExpectationError)
104
-
105
- reset_all
106
108
  end
107
109
 
108
110
  it "reports the line number of expectation of unreceived message", :pending => options.include?(:does_not_report_line_num) do
@@ -296,7 +298,7 @@ module RSpec
296
298
  context "when rspec-expectations is included in the test framework first" do
297
299
  before do
298
300
  # the examples here assume `expect` is define in RSpec::Matchers...
299
- expect(RSpec::Matchers.method_defined?(:expect)).to be_true
301
+ expect(RSpec::Matchers.method_defined?(:expect)).to be true
300
302
  end
301
303
 
302
304
  let(:framework) do
@@ -318,7 +320,7 @@ module RSpec
318
320
  context "when rspec-expectations is included in the test framework last" do
319
321
  before do
320
322
  # the examples here assume `expect` is define in RSpec::Matchers...
321
- expect(RSpec::Matchers.method_defined?(:expect)).to be_true
323
+ expect(RSpec::Matchers.method_defined?(:expect)).to be true
322
324
  end
323
325
 
324
326
  let(:framework) do
@@ -97,6 +97,16 @@ module RSpec
97
97
  expect(r1).to be(r2)
98
98
  expect(r1).not_to be(r3)
99
99
  end
100
+
101
+ it 'removes an any_instance_recorder when requested' do
102
+ klass = Class.new
103
+
104
+ space.any_instance_recorder_for(klass)
105
+
106
+ expect {
107
+ space.remove_any_instance_recorder_for(klass)
108
+ }.to change { space.any_instance_recorders.size }.by(-1)
109
+ end
100
110
  end
101
111
  end
102
112
  end
@@ -122,6 +122,25 @@ module RSpec
122
122
  verify @double
123
123
  end
124
124
 
125
+ it 'does not get confused when `should_not_received` is used with a string and symbol message' do
126
+ @double.stub(:foo) { 3 }
127
+ @double.should_not_receive(:foo).with(1)
128
+ @double.should_not_receive("foo").with(2)
129
+
130
+ expect(@double.foo).to eq(3)
131
+ verify @double
132
+ end
133
+
134
+ it 'does not get confused when `should_received` is used with a string and symbol message' do
135
+ @double.should_receive(:foo).with(1)
136
+ @double.should_receive("foo").with(2)
137
+
138
+ @double.foo(1)
139
+ @double.foo(2)
140
+
141
+ verify @double
142
+ end
143
+
125
144
  it "allows block to calculate return values" do
126
145
  @double.should_receive(:something).with("a","b","c").and_return { |a,b,c| c+b+a }
127
146
  expect(@double.something("a","b","c")).to eq "cba"
@@ -215,7 +234,7 @@ module RSpec
215
234
 
216
235
  it "fails if expectation block fails" do
217
236
  @double.should_receive(:something) do |bool|
218
- expect(bool).to be_true
237
+ expect(bool).to be true
219
238
  end
220
239
 
221
240
  expect {
@@ -78,18 +78,18 @@ module RSpec
78
78
 
79
79
  shared_examples_for "loaded constant hiding" do |const_name|
80
80
  before do
81
- expect(recursive_const_defined?(const_name)).to be_true
81
+ expect(recursive_const_defined?(const_name)).to be_truthy
82
82
  end
83
83
 
84
84
  it 'allows it to be hidden' do
85
85
  hide_const(const_name)
86
- expect(recursive_const_defined?(const_name)).to be_false
86
+ expect(recursive_const_defined?(const_name)).to be_falsey
87
87
  end
88
88
 
89
89
  it 'resets the constant when rspec clear its mocks' do
90
90
  hide_const(const_name)
91
91
  reset_rspec_mocks
92
- expect(recursive_const_defined?(const_name)).to be_true
92
+ expect(recursive_const_defined?(const_name)).to be_truthy
93
93
  end
94
94
 
95
95
  it 'returns nil' do
@@ -101,7 +101,7 @@ module RSpec
101
101
  include_context "constant example methods", const_name
102
102
 
103
103
  before do
104
- expect(recursive_const_defined?(const_name)).to be_false
104
+ expect(recursive_const_defined?(const_name)).to be_falsey
105
105
  end
106
106
 
107
107
  it 'allows it to be stubbed' do
@@ -112,7 +112,7 @@ module RSpec
112
112
  it 'removes the constant when rspec clears its mocks' do
113
113
  stub_const(const_name, 7)
114
114
  reset_rspec_mocks
115
- expect(recursive_const_defined?(const_name)).to be_false
115
+ expect(recursive_const_defined?(const_name)).to be_falsey
116
116
  end
117
117
 
118
118
  it 'returns the stubbed value' do
@@ -130,18 +130,18 @@ module RSpec
130
130
  include_context "constant example methods", const_name
131
131
 
132
132
  before do
133
- expect(recursive_const_defined?(const_name)).to be_false
133
+ expect(recursive_const_defined?(const_name)).to be_falsey
134
134
  end
135
135
 
136
136
  it 'allows it to be hidden, though the operation has no effect' do
137
137
  hide_const(const_name)
138
- expect(recursive_const_defined?(const_name)).to be_false
138
+ expect(recursive_const_defined?(const_name)).to be_falsey
139
139
  end
140
140
 
141
141
  it 'remains undefined after rspec clears its mocks' do
142
142
  hide_const(const_name)
143
143
  reset_rspec_mocks
144
- expect(recursive_const_defined?(const_name)).to be_false
144
+ expect(recursive_const_defined?(const_name)).to be_falsey
145
145
  end
146
146
 
147
147
  it 'returns nil' do
@@ -206,7 +206,7 @@ module RSpec
206
206
  orig_value = TOP_LEVEL_VALUE_CONST
207
207
 
208
208
  hide_const("TOP_LEVEL_VALUE_CONST")
209
- expect(recursive_const_defined?("TOP_LEVEL_VALUE_CONST")).to be_false
209
+ expect(recursive_const_defined?("TOP_LEVEL_VALUE_CONST")).to be_falsey
210
210
 
211
211
  stub_const("TOP_LEVEL_VALUE_CONST", 12345)
212
212
  expect(TOP_LEVEL_VALUE_CONST).to eq 12345
@@ -247,8 +247,8 @@ module RSpec
247
247
  stub = Module.new
248
248
  stub_const("TestSubClass", stub, :transfer_nested_constants => true)
249
249
  expect(stub::P).to eq(:p)
250
- expect(defined?(stub::M)).to be_false
251
- expect(defined?(stub::N)).to be_false
250
+ expect(defined?(stub::M)).to be_falsey
251
+ expect(defined?(stub::N)).to be_falsey
252
252
  end
253
253
 
254
254
  it 'raises an error when asked to transfer a nested inherited constant' do
@@ -266,7 +266,7 @@ module RSpec
266
266
  stub_const("TestClass", stub, :transfer_nested_constants => [:M, :N])
267
267
  expect(stub::M).to eq(:m)
268
268
  expect(stub::N).to eq(:n)
269
- expect(defined?(stub::Nested)).to be_false
269
+ expect(defined?(stub::Nested)).to be_falsey
270
270
  end
271
271
 
272
272
  it 'raises an error if asked to transfer nested constants but given an object that does not support them' do
@@ -302,7 +302,7 @@ module RSpec
302
302
 
303
303
  it 'raises an error if asked to transfer a nested constant that is not defined' do
304
304
  original_tc = TestClass
305
- expect(defined?(TestClass::V)).to be_false
305
+ expect(defined?(TestClass::V)).to be_falsey
306
306
  stub = Module.new
307
307
 
308
308
  expect {
@@ -345,10 +345,10 @@ module RSpec
345
345
  it_behaves_like "unloaded constant stubbing", "X::Y"
346
346
 
347
347
  it 'removes the root constant when rspec clears its mocks' do
348
- expect(defined?(X)).to be_false
348
+ expect(defined?(X)).to be_falsey
349
349
  stub_const("X::Y", 7)
350
350
  reset_rspec_mocks
351
- expect(defined?(X)).to be_false
351
+ expect(defined?(X)).to be_falsey
352
352
  end
353
353
  end
354
354
 
@@ -356,10 +356,10 @@ module RSpec
356
356
  it_behaves_like "unloaded constant stubbing", "X::Y::Z"
357
357
 
358
358
  it 'removes the root constant when rspec clears its mocks' do
359
- expect(defined?(X)).to be_false
359
+ expect(defined?(X)).to be_falsey
360
360
  stub_const("X::Y::Z", 7)
361
361
  reset_rspec_mocks
362
- expect(defined?(X)).to be_false
362
+ expect(defined?(X)).to be_falsey
363
363
  end
364
364
  end
365
365
 
@@ -367,12 +367,12 @@ module RSpec
367
367
  it_behaves_like "unloaded constant stubbing", "TestClass::X"
368
368
 
369
369
  it 'removes the unloaded constant but leaves the loaded constant when rspec resets its mocks' do
370
- expect(defined?(TestClass)).to be_true
371
- expect(defined?(TestClass::X)).to be_false
370
+ expect(defined?(TestClass)).to be_truthy
371
+ expect(defined?(TestClass::X)).to be_falsey
372
372
  stub_const("TestClass::X", 7)
373
373
  reset_rspec_mocks
374
- expect(defined?(TestClass)).to be_true
375
- expect(defined?(TestClass::X)).to be_false
374
+ expect(defined?(TestClass)).to be_truthy
375
+ expect(defined?(TestClass::X)).to be_falsey
376
376
  end
377
377
 
378
378
  it 'raises a helpful error if it cannot be stubbed due to an intermediary constant that is not a module' do
@@ -385,12 +385,12 @@ module RSpec
385
385
  it_behaves_like "unloaded constant stubbing", "TestClass::Nested::NestedEvenMore::X::Y::Z"
386
386
 
387
387
  it 'removes the first unloaded constant but leaves the loaded nested constant when rspec resets its mocks' do
388
- expect(defined?(TestClass::Nested::NestedEvenMore)).to be_true
389
- expect(defined?(TestClass::Nested::NestedEvenMore::X)).to be_false
388
+ expect(defined?(TestClass::Nested::NestedEvenMore)).to be_truthy
389
+ expect(defined?(TestClass::Nested::NestedEvenMore::X)).to be_falsey
390
390
  stub_const("TestClass::Nested::NestedEvenMore::X::Y::Z", 7)
391
391
  reset_rspec_mocks
392
- expect(defined?(TestClass::Nested::NestedEvenMore)).to be_true
393
- expect(defined?(TestClass::Nested::NestedEvenMore::X)).to be_false
392
+ expect(defined?(TestClass::Nested::NestedEvenMore)).to be_truthy
393
+ expect(defined?(TestClass::Nested::NestedEvenMore::X)).to be_falsey
394
394
  end
395
395
  end
396
396
  end