rspec-mocks 2.14.6 → 2.99.0.beta1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +8 -8
- data/Changelog.md +20 -14
- data/lib/rspec/mocks.rb +6 -2
- data/lib/rspec/mocks/any_instance/chain.rb +28 -0
- data/lib/rspec/mocks/any_instance/expectation_chain.rb +5 -10
- data/lib/rspec/mocks/any_instance/recorder.rb +4 -1
- data/lib/rspec/mocks/any_instance/stub_chain.rb +5 -5
- data/lib/rspec/mocks/caller_filter.rb +55 -0
- data/lib/rspec/mocks/configuration.rb +19 -0
- data/lib/rspec/mocks/deprecation.rb +9 -1
- data/lib/rspec/mocks/extensions/proc.rb +63 -0
- data/lib/rspec/mocks/framework.rb +2 -0
- data/lib/rspec/mocks/matchers/receive.rb +2 -9
- data/lib/rspec/mocks/message_expectation.rb +92 -22
- data/lib/rspec/mocks/method_double.rb +2 -2
- data/lib/rspec/mocks/proxy_for_nil.rb +2 -2
- data/lib/rspec/mocks/space.rb +4 -5
- data/lib/rspec/mocks/stub_chain.rb +1 -1
- data/lib/rspec/mocks/syntax.rb +3 -3
- data/lib/rspec/mocks/targets.rb +1 -1
- data/lib/rspec/mocks/test_double.rb +8 -2
- data/lib/rspec/mocks/version.rb +1 -1
- data/spec/rspec/mocks/and_yield_spec.rb +1 -1
- data/spec/rspec/mocks/any_instance/message_chains_spec.rb +3 -3
- data/spec/rspec/mocks/any_instance_spec.rb +233 -84
- data/spec/rspec/mocks/argument_expectation_spec.rb +4 -4
- data/spec/rspec/mocks/block_return_value_spec.rb +49 -11
- data/spec/rspec/mocks/bug_report_10263_spec.rb +1 -1
- data/spec/rspec/mocks/bug_report_8165_spec.rb +2 -2
- data/spec/rspec/mocks/combining_implementation_instructions_spec.rb +4 -4
- data/spec/rspec/mocks/double_spec.rb +7 -0
- data/spec/rspec/mocks/failing_argument_matchers_spec.rb +1 -0
- data/spec/rspec/mocks/matchers/receive_spec.rb +10 -8
- data/spec/rspec/mocks/mock_space_spec.rb +10 -0
- data/spec/rspec/mocks/mock_spec.rb +20 -1
- data/spec/rspec/mocks/mutate_const_spec.rb +25 -25
- data/spec/rspec/mocks/null_object_mock_spec.rb +7 -0
- data/spec/rspec/mocks/passing_argument_matchers_spec.rb +4 -2
- data/spec/rspec/mocks/record_messages_spec.rb +4 -4
- data/spec/rspec/mocks/space_spec.rb +1 -1
- data/spec/rspec/mocks/stub_chain_spec.rb +8 -12
- data/spec/rspec/mocks/syntax_agnostic_message_matchers_spec.rb +20 -0
- data/spec/rspec/mocks/test_double_spec.rb +0 -5
- data/spec/rspec/mocks_spec.rb +14 -0
- data/spec/spec_helper.rb +27 -0
- metadata +7 -5
@@ -147,7 +147,7 @@ module RSpec
|
|
147
147
|
|used instead; however, it may not work correctly when executed due
|
148
148
|
|to the fact that `self` will be #{@object.superclass}, not #{@object}.
|
149
149
|
|
|
150
|
-
|Called from: #{
|
150
|
+
|Called from: #{CallerFilter.first_non_rspec_line}
|
151
151
|
WARNING
|
152
152
|
|
153
153
|
@object.superclass.method(@method_name)
|
@@ -197,7 +197,7 @@ module RSpec
|
|
197
197
|
# @private
|
198
198
|
def restore_original_visibility
|
199
199
|
return unless object_singleton_class.method_defined?(@method_name) || object_singleton_class.private_method_defined?(@method_name)
|
200
|
-
object_singleton_class.class_eval(@original_visibility, __FILE__, __LINE__)
|
200
|
+
object_singleton_class.class_eval(@original_visibility, __FILE__, __LINE__ + 1)
|
201
201
|
end
|
202
202
|
|
203
203
|
# @private
|
@@ -28,8 +28,8 @@ module RSpec
|
|
28
28
|
private
|
29
29
|
|
30
30
|
def warn method_name
|
31
|
-
|
32
|
-
Kernel.warn("An expectation of :#{method_name} was set on nil. Called from #{
|
31
|
+
source = CallerFilter.first_non_rspec_line
|
32
|
+
Kernel.warn("An expectation of :#{method_name} was set on nil. Called from #{source}. Use allow_message_expectations_on_nil to disable warnings.")
|
33
33
|
end
|
34
34
|
|
35
35
|
end
|
data/lib/rspec/mocks/space.rb
CHANGED
@@ -27,11 +27,6 @@ module RSpec
|
|
27
27
|
end
|
28
28
|
|
29
29
|
proxies.clear
|
30
|
-
|
31
|
-
any_instance_recorders.each_value do |recorder|
|
32
|
-
recorder.stop_all_observation!
|
33
|
-
end
|
34
|
-
|
35
30
|
any_instance_recorders.clear
|
36
31
|
expectation_ordering.clear
|
37
32
|
end
|
@@ -47,6 +42,10 @@ module RSpec
|
|
47
42
|
end
|
48
43
|
end
|
49
44
|
|
45
|
+
def remove_any_instance_recorder_for(klass)
|
46
|
+
any_instance_recorders.delete(klass.__id__)
|
47
|
+
end
|
48
|
+
|
50
49
|
def proxies_of(klass)
|
51
50
|
proxies.values.select { |proxy| klass === proxy.object }
|
52
51
|
end
|
data/lib/rspec/mocks/syntax.rb
CHANGED
@@ -15,7 +15,7 @@ module RSpec
|
|
15
15
|
if ::Hash === message_or_hash
|
16
16
|
message_or_hash.each {|message, value| stub_object(object, message).and_return value }
|
17
17
|
else
|
18
|
-
opts[:expected_from] =
|
18
|
+
opts[:expected_from] = CallerFilter.first_non_rspec_line
|
19
19
|
::RSpec::Mocks.allow_message(object, message_or_hash, opts, &block)
|
20
20
|
end
|
21
21
|
end
|
@@ -27,12 +27,12 @@ module RSpec
|
|
27
27
|
|
28
28
|
syntax_host.class_eval do
|
29
29
|
def should_receive(message, opts={}, &block)
|
30
|
-
opts[:expected_from] ||=
|
30
|
+
opts[:expected_from] ||= CallerFilter.first_non_rspec_line
|
31
31
|
::RSpec::Mocks.expect_message(self, message.to_sym, opts, &block)
|
32
32
|
end
|
33
33
|
|
34
34
|
def should_not_receive(message, &block)
|
35
|
-
opts = {:expected_from =>
|
35
|
+
opts = { :expected_from => CallerFilter.first_non_rspec_line }
|
36
36
|
::RSpec::Mocks.expect_message(self, message.to_sym, opts, &block).never
|
37
37
|
end
|
38
38
|
|
data/lib/rspec/mocks/targets.rb
CHANGED
@@ -10,7 +10,7 @@ module RSpec
|
|
10
10
|
|
11
11
|
def self.delegate_to(matcher_method, options = {})
|
12
12
|
method_name = options.fetch(:from) { :to }
|
13
|
-
class_eval(<<-RUBY)
|
13
|
+
class_eval(<<-RUBY, __FILE__, __LINE__ + 1)
|
14
14
|
def #{method_name}(matcher, &block)
|
15
15
|
unless Matchers::Receive === matcher
|
16
16
|
raise UnsupportedMatcherError, "only the `receive` matcher is supported " +
|
@@ -27,8 +27,9 @@ module RSpec
|
|
27
27
|
# are declared, they'll work as expected. If not, the receiver is
|
28
28
|
# returned.
|
29
29
|
def as_null_object
|
30
|
-
@__null_object = true
|
31
30
|
__mock_proxy.as_null_object
|
31
|
+
@__null_object = true
|
32
|
+
self
|
32
33
|
end
|
33
34
|
|
34
35
|
# Returns true if this object has received `as_null_object`
|
@@ -64,7 +65,12 @@ module RSpec
|
|
64
65
|
# @private
|
65
66
|
def __build_mock_proxy
|
66
67
|
proxy = Proxy.new(self, @name, @options || {})
|
67
|
-
|
68
|
+
|
69
|
+
if null_object?
|
70
|
+
proxy.as_null_object
|
71
|
+
RSpec.deprecate "Relying on a test double's null-ness to persist between examples"
|
72
|
+
end
|
73
|
+
|
68
74
|
proxy
|
69
75
|
end
|
70
76
|
|
data/lib/rspec/mocks/version.rb
CHANGED
@@ -25,7 +25,7 @@ describe RSpec::Mocks::Mock do
|
|
25
25
|
obj.stub(:method_that_accepts_a_block).and_yield do |eval_context|
|
26
26
|
evaluated = true
|
27
27
|
end
|
28
|
-
expect(evaluated).to
|
28
|
+
expect(evaluated).to be true
|
29
29
|
end
|
30
30
|
|
31
31
|
it "passes an eval context object to the supplied block" do
|
@@ -4,17 +4,17 @@ describe RSpec::Mocks::AnyInstance::MessageChains do
|
|
4
4
|
let(:recorder) { double }
|
5
5
|
let(:chains) { RSpec::Mocks::AnyInstance::MessageChains.new }
|
6
6
|
let(:stub_chain) { RSpec::Mocks::AnyInstance::StubChain.new recorder }
|
7
|
-
let(:expectation_chain) { RSpec::Mocks::AnyInstance::
|
7
|
+
let(:expectation_chain) { RSpec::Mocks::AnyInstance::ExpectationChain.new recorder }
|
8
8
|
|
9
9
|
it "knows if a method does not have an expectation set on it" do
|
10
10
|
chains.add(:method_name, stub_chain)
|
11
|
-
expect(chains.has_expectation?(:method_name)).to
|
11
|
+
expect(chains.has_expectation?(:method_name)).to be_falsey
|
12
12
|
end
|
13
13
|
|
14
14
|
it "knows if a method has an expectation set on it" do
|
15
15
|
chains.add(:method_name, stub_chain)
|
16
16
|
chains.add(:method_name, expectation_chain)
|
17
|
-
expect(chains.has_expectation?(:method_name)).to
|
17
|
+
expect(chains.has_expectation?(:method_name)).to be_truthy
|
18
18
|
end
|
19
19
|
|
20
20
|
it "can remove all stub chains" do
|
@@ -332,6 +332,12 @@ module RSpec
|
|
332
332
|
klass.any_instance.unstub(:existing_method)
|
333
333
|
}.to raise_error(RSpec::Mocks::MockExpectationError, 'The method `existing_method` was not stubbed or was already unstubbed')
|
334
334
|
end
|
335
|
+
|
336
|
+
it 'does not get confused about string vs symbol usage for the message' do
|
337
|
+
klass.any_instance.stub(:existing_method) { :stubbed }
|
338
|
+
klass.any_instance.unstub("existing_method")
|
339
|
+
expect(klass.new.existing_method).to eq(:existing_method_return_value)
|
340
|
+
end
|
335
341
|
end
|
336
342
|
|
337
343
|
context "with #should_not_receive" do
|
@@ -369,7 +375,6 @@ module RSpec
|
|
369
375
|
klass.any_instance.should_not_receive(:bar)
|
370
376
|
klass.new.foo
|
371
377
|
RSpec::Mocks.space.verify_all
|
372
|
-
RSpec::Mocks.space.reset_all
|
373
378
|
end
|
374
379
|
end
|
375
380
|
|
@@ -392,36 +397,24 @@ module RSpec
|
|
392
397
|
|
393
398
|
it "fails if an instance is created but no invocation occurs" do
|
394
399
|
expect do
|
395
|
-
|
396
|
-
|
397
|
-
|
398
|
-
RSpec::Mocks.space.verify_all
|
399
|
-
ensure
|
400
|
-
RSpec::Mocks.space.reset_all
|
401
|
-
end
|
400
|
+
klass.any_instance.should_receive(:foo)
|
401
|
+
klass.new
|
402
|
+
RSpec::Mocks.space.verify_all
|
402
403
|
end.to raise_error(RSpec::Mocks::MockExpectationError, foo_expectation_error_message)
|
403
404
|
end
|
404
405
|
|
405
406
|
it "fails if no instance is created" do
|
406
407
|
expect do
|
407
|
-
|
408
|
-
|
409
|
-
RSpec::Mocks.space.verify_all
|
410
|
-
ensure
|
411
|
-
RSpec::Mocks.space.reset_all
|
412
|
-
end
|
408
|
+
klass.any_instance.should_receive(:foo).and_return(1)
|
409
|
+
RSpec::Mocks.space.verify_all
|
413
410
|
end.to raise_error(RSpec::Mocks::MockExpectationError, foo_expectation_error_message)
|
414
411
|
end
|
415
412
|
|
416
413
|
it "fails if no instance is created and there are multiple expectations" do
|
417
414
|
expect do
|
418
|
-
|
419
|
-
|
420
|
-
|
421
|
-
RSpec::Mocks.space.verify_all
|
422
|
-
ensure
|
423
|
-
RSpec::Mocks.space.reset_all
|
424
|
-
end
|
415
|
+
klass.any_instance.should_receive(:foo)
|
416
|
+
klass.any_instance.should_receive(:bar)
|
417
|
+
RSpec::Mocks.space.verify_all
|
425
418
|
end.to raise_error(RSpec::Mocks::MockExpectationError, 'Exactly one instance should have received the following message(s) but didn\'t: bar, foo')
|
426
419
|
end
|
427
420
|
|
@@ -483,36 +476,24 @@ module RSpec
|
|
483
476
|
|
484
477
|
it "fails if an instance is created but no invocation occurs" do
|
485
478
|
expect do
|
486
|
-
|
487
|
-
|
488
|
-
|
489
|
-
RSpec::Mocks.space.verify_all
|
490
|
-
ensure
|
491
|
-
RSpec::Mocks.space.reset_all
|
492
|
-
end
|
479
|
+
klass.any_instance.should_receive(:existing_method)
|
480
|
+
klass.new
|
481
|
+
RSpec::Mocks.space.verify_all
|
493
482
|
end.to raise_error(RSpec::Mocks::MockExpectationError, existing_method_expectation_error_message)
|
494
483
|
end
|
495
484
|
|
496
485
|
it "fails if no instance is created" do
|
497
486
|
expect do
|
498
|
-
|
499
|
-
|
500
|
-
RSpec::Mocks.space.verify_all
|
501
|
-
ensure
|
502
|
-
RSpec::Mocks.space.reset_all
|
503
|
-
end
|
487
|
+
klass.any_instance.should_receive(:existing_method)
|
488
|
+
RSpec::Mocks.space.verify_all
|
504
489
|
end.to raise_error(RSpec::Mocks::MockExpectationError, existing_method_expectation_error_message)
|
505
490
|
end
|
506
491
|
|
507
492
|
it "fails if no instance is created and there are multiple expectations" do
|
508
493
|
expect do
|
509
|
-
|
510
|
-
|
511
|
-
|
512
|
-
RSpec::Mocks.space.verify_all
|
513
|
-
ensure
|
514
|
-
RSpec::Mocks.space.reset_all
|
515
|
-
end
|
494
|
+
klass.any_instance.should_receive(:existing_method)
|
495
|
+
klass.any_instance.should_receive(:another_existing_method)
|
496
|
+
RSpec::Mocks.space.verify_all
|
516
497
|
end.to raise_error(RSpec::Mocks::MockExpectationError, 'Exactly one instance should have received the following message(s) but didn\'t: another_existing_method, existing_method')
|
517
498
|
end
|
518
499
|
|
@@ -605,24 +586,16 @@ module RSpec
|
|
605
586
|
|
606
587
|
it "fails when no instances are declared" do
|
607
588
|
expect do
|
608
|
-
|
609
|
-
|
610
|
-
RSpec::Mocks.space.verify_all
|
611
|
-
ensure
|
612
|
-
RSpec::Mocks.space.reset_all
|
613
|
-
end
|
589
|
+
klass.any_instance.should_receive(:foo).once
|
590
|
+
RSpec::Mocks.space.verify_all
|
614
591
|
end.to raise_error(RSpec::Mocks::MockExpectationError, foo_expectation_error_message)
|
615
592
|
end
|
616
593
|
|
617
594
|
it "fails when an instance is declared but there are no invocations" do
|
618
595
|
expect do
|
619
|
-
|
620
|
-
|
621
|
-
|
622
|
-
RSpec::Mocks.space.verify_all
|
623
|
-
ensure
|
624
|
-
RSpec::Mocks.space.reset_all
|
625
|
-
end
|
596
|
+
klass.any_instance.should_receive(:foo).once
|
597
|
+
klass.new
|
598
|
+
RSpec::Mocks.space.verify_all
|
626
599
|
end.to raise_error(RSpec::Mocks::MockExpectationError, foo_expectation_error_message)
|
627
600
|
end
|
628
601
|
|
@@ -747,13 +720,9 @@ module RSpec
|
|
747
720
|
|
748
721
|
it "fails when the other expecations are not met" do
|
749
722
|
expect do
|
750
|
-
|
751
|
-
|
752
|
-
|
753
|
-
RSpec::Mocks.space.verify_all
|
754
|
-
ensure
|
755
|
-
RSpec::Mocks.space.reset_all
|
756
|
-
end
|
723
|
+
klass.any_instance.should_receive(:foo).never
|
724
|
+
klass.any_instance.should_receive(:existing_method).and_return(5)
|
725
|
+
RSpec::Mocks.space.verify_all
|
757
726
|
end.to raise_error(RSpec::Mocks::MockExpectationError, existing_method_expectation_error_message)
|
758
727
|
end
|
759
728
|
end
|
@@ -789,13 +758,9 @@ module RSpec
|
|
789
758
|
|
790
759
|
it "fails when the other expecations are not met" do
|
791
760
|
expect do
|
792
|
-
|
793
|
-
|
794
|
-
|
795
|
-
RSpec::Mocks.space.verify_all
|
796
|
-
ensure
|
797
|
-
RSpec::Mocks.space.reset_all
|
798
|
-
end
|
761
|
+
klass.any_instance.should_receive(:foo).any_number_of_times
|
762
|
+
klass.any_instance.should_receive(:existing_method).and_return(5)
|
763
|
+
RSpec::Mocks.space.verify_all
|
799
764
|
end.to raise_error(RSpec::Mocks::MockExpectationError, existing_method_expectation_error_message)
|
800
765
|
end
|
801
766
|
end
|
@@ -815,14 +780,13 @@ module RSpec
|
|
815
780
|
context "public methods" do
|
816
781
|
before(:each) do
|
817
782
|
klass.any_instance.stub(:existing_method).and_return(1)
|
818
|
-
expect(klass.method_defined?(:__existing_method_without_any_instance__)).to
|
783
|
+
expect(klass.method_defined?(:__existing_method_without_any_instance__)).to be true
|
819
784
|
end
|
820
785
|
|
821
786
|
it "restores the class to its original state after each example when no instance is created" do
|
822
787
|
space.verify_all
|
823
|
-
space.reset_all
|
824
788
|
|
825
|
-
expect(klass.method_defined?(:__existing_method_without_any_instance__)).to
|
789
|
+
expect(klass.method_defined?(:__existing_method_without_any_instance__)).to be false
|
826
790
|
expect(klass.new.existing_method).to eq(existing_method_return_value)
|
827
791
|
end
|
828
792
|
|
@@ -830,9 +794,8 @@ module RSpec
|
|
830
794
|
klass.new.existing_method
|
831
795
|
|
832
796
|
space.verify_all
|
833
|
-
space.reset_all
|
834
797
|
|
835
|
-
expect(klass.method_defined?(:__existing_method_without_any_instance__)).to
|
798
|
+
expect(klass.method_defined?(:__existing_method_without_any_instance__)).to be false
|
836
799
|
expect(klass.new.existing_method).to eq(existing_method_return_value)
|
837
800
|
end
|
838
801
|
|
@@ -841,9 +804,8 @@ module RSpec
|
|
841
804
|
klass.new.existing_method
|
842
805
|
|
843
806
|
space.verify_all
|
844
|
-
space.reset_all
|
845
807
|
|
846
|
-
expect(klass.method_defined?(:__existing_method_without_any_instance__)).to
|
808
|
+
expect(klass.method_defined?(:__existing_method_without_any_instance__)).to be false
|
847
809
|
expect(klass.new.existing_method).to eq(existing_method_return_value)
|
848
810
|
end
|
849
811
|
end
|
@@ -852,15 +814,14 @@ module RSpec
|
|
852
814
|
before :each do
|
853
815
|
klass.any_instance.stub(:private_method).and_return(:something)
|
854
816
|
space.verify_all
|
855
|
-
space.reset_all
|
856
817
|
end
|
857
818
|
|
858
819
|
it "cleans up the backed up method" do
|
859
|
-
expect(klass.method_defined?(:__existing_method_without_any_instance__)).to
|
820
|
+
expect(klass.method_defined?(:__existing_method_without_any_instance__)).to be false
|
860
821
|
end
|
861
822
|
|
862
823
|
it "restores a stubbed private method after the spec is run" do
|
863
|
-
expect(klass.private_method_defined?(:private_method)).to
|
824
|
+
expect(klass.private_method_defined?(:private_method)).to be true
|
864
825
|
end
|
865
826
|
|
866
827
|
it "ensures that the restored method behaves as it originally did" do
|
@@ -875,15 +836,14 @@ module RSpec
|
|
875
836
|
klass.any_instance.should_receive(:private_method).and_return(:something)
|
876
837
|
klass.new.private_method
|
877
838
|
space.verify_all
|
878
|
-
space.reset_all
|
879
839
|
end
|
880
840
|
|
881
841
|
it "cleans up the backed up method" do
|
882
|
-
expect(klass.method_defined?(:__existing_method_without_any_instance__)).to
|
842
|
+
expect(klass.method_defined?(:__existing_method_without_any_instance__)).to be false
|
883
843
|
end
|
884
844
|
|
885
845
|
it "restores a stubbed private method after the spec is run" do
|
886
|
-
expect(klass.private_method_defined?(:private_method)).to
|
846
|
+
expect(klass.private_method_defined?(:private_method)).to be true
|
887
847
|
end
|
888
848
|
|
889
849
|
it "ensures that the restored method behaves as it originally did" do
|
@@ -923,7 +883,6 @@ module RSpec
|
|
923
883
|
klass.any_instance.should_receive(:existing_method).and_return(Object.new)
|
924
884
|
klass.new.existing_method
|
925
885
|
space.verify_all
|
926
|
-
space.reset_all
|
927
886
|
|
928
887
|
expect(klass.new.existing_method).to eq(existing_method_return_value)
|
929
888
|
end
|
@@ -936,7 +895,6 @@ module RSpec
|
|
936
895
|
klass.any_instance.stub(:existing_method).and_return(true)
|
937
896
|
|
938
897
|
RSpec::Mocks.space.verify_all
|
939
|
-
RSpec::Mocks.space.reset_all
|
940
898
|
expect(klass.new).to respond_to(:existing_method)
|
941
899
|
expect(klass.new.existing_method).to eq(existing_method_return_value)
|
942
900
|
end
|
@@ -956,6 +914,198 @@ module RSpec
|
|
956
914
|
end
|
957
915
|
end
|
958
916
|
|
917
|
+
context "passing the receiver to the implementation block" do
|
918
|
+
context "when configured to pass the instance" do
|
919
|
+
include_context 'with isolated configuration'
|
920
|
+
before(:each) do
|
921
|
+
RSpec::Mocks.configuration.yield_receiver_to_any_instance_implementation_blocks = true
|
922
|
+
end
|
923
|
+
|
924
|
+
describe "an any instance stub" do
|
925
|
+
it "passes the instance as the first arg of the implementation block" do
|
926
|
+
instance = klass.new
|
927
|
+
|
928
|
+
expect { |b|
|
929
|
+
klass.any_instance.should_receive(:bees).with(:sup, &b)
|
930
|
+
instance.bees(:sup)
|
931
|
+
}.to yield_with_args(instance, :sup)
|
932
|
+
end
|
933
|
+
|
934
|
+
it "does not pass the instance to and_call_original" do
|
935
|
+
klass = Class.new do
|
936
|
+
def call(*args)
|
937
|
+
args.first
|
938
|
+
end
|
939
|
+
end
|
940
|
+
klass.any_instance.should_receive(:call).and_call_original
|
941
|
+
instance = klass.new
|
942
|
+
expect(instance.call(:bees)).to be :bees
|
943
|
+
end
|
944
|
+
end
|
945
|
+
|
946
|
+
describe "an any instance expectation" do
|
947
|
+
it "doesn't effect with" do
|
948
|
+
instance = klass.new
|
949
|
+
klass.any_instance.should_receive(:bees).with(:sup)
|
950
|
+
instance.bees(:sup)
|
951
|
+
end
|
952
|
+
|
953
|
+
it "passes the instance as the first arg of the implementation block" do
|
954
|
+
instance = klass.new
|
955
|
+
|
956
|
+
expect { |b|
|
957
|
+
klass.any_instance.should_receive(:bees).with(:sup, &b)
|
958
|
+
instance.bees(:sup)
|
959
|
+
}.to yield_with_args(instance, :sup)
|
960
|
+
end
|
961
|
+
end
|
962
|
+
end
|
963
|
+
|
964
|
+
context "when configured not to pass the instance" do
|
965
|
+
include_context 'with isolated configuration'
|
966
|
+
before(:each) do
|
967
|
+
RSpec::Mocks.configuration.yield_receiver_to_any_instance_implementation_blocks = false
|
968
|
+
end
|
969
|
+
|
970
|
+
describe "an any instance stub" do
|
971
|
+
it "does not pass the instance to the implementation block" do
|
972
|
+
instance = klass.new
|
973
|
+
|
974
|
+
expect { |b|
|
975
|
+
klass.any_instance.should_receive(:bees).with(:sup, &b)
|
976
|
+
instance.bees(:sup)
|
977
|
+
}.to yield_with_args(:sup)
|
978
|
+
end
|
979
|
+
|
980
|
+
it "does not cause with to fail when the instance is passed" do
|
981
|
+
instance = klass.new
|
982
|
+
klass.any_instance.should_receive(:bees).with(:faces)
|
983
|
+
instance.bees(:faces)
|
984
|
+
end
|
985
|
+
end
|
986
|
+
end
|
987
|
+
|
988
|
+
context "by default" do
|
989
|
+
def block_regex(line)
|
990
|
+
/as the first block argument.*#{__FILE__}:#{line}/m
|
991
|
+
end
|
992
|
+
|
993
|
+
it "is off" do
|
994
|
+
expect(RSpec::Mocks.configuration.yield_receiver_to_any_instance_implementation_blocks?).to be false
|
995
|
+
end
|
996
|
+
|
997
|
+
it "will warn about allowances receiving blocks in 3.0" do
|
998
|
+
klass = Struct.new(:bees)
|
999
|
+
|
1000
|
+
expect(RSpec).to receive(:warn_deprecation).with(block_regex(__LINE__ + 1))
|
1001
|
+
allow_any_instance_of(klass).to receive(:foo) { |args| }
|
1002
|
+
klass.new(:faces).foo
|
1003
|
+
end
|
1004
|
+
|
1005
|
+
it "will warn about expectations receiving blocks in 3.0" do
|
1006
|
+
klass = Struct.new(:bees)
|
1007
|
+
|
1008
|
+
expect(RSpec).to receive(:warn_deprecation).with(block_regex(__LINE__ + 1))
|
1009
|
+
expect_any_instance_of(klass).to receive(:foo) { |args| }
|
1010
|
+
klass.new(:faces).foo
|
1011
|
+
end
|
1012
|
+
|
1013
|
+
it 'includes the line of the block declaration in the warning, ' +
|
1014
|
+
'even when it is different from the `any_instance` line', :unless => (RUBY_VERSION.to_f < 1.9) do
|
1015
|
+
klass = Class.new
|
1016
|
+
expect(RSpec).to receive(:warn_deprecation).with(block_regex(__LINE__ + 3))
|
1017
|
+
stub = klass.any_instance.stub(:foo)
|
1018
|
+
|
1019
|
+
stub.with("bar") { |args| }
|
1020
|
+
klass.new.foo("bar")
|
1021
|
+
end
|
1022
|
+
|
1023
|
+
it "will warn about expectations receiving blocks with a times restriction" do
|
1024
|
+
klass = Struct.new(:bees)
|
1025
|
+
|
1026
|
+
expect(RSpec).to receive(:warn_deprecation).with(block_regex(__LINE__ + 1))
|
1027
|
+
klass.any_instance.should_receive(:foo).exactly(3).times { |a| :some_return_value }
|
1028
|
+
|
1029
|
+
instance = klass.new(:faces)
|
1030
|
+
3.times { instance.foo }
|
1031
|
+
end
|
1032
|
+
|
1033
|
+
it "will warn about expectations receiving blocks with an argument expectation" do
|
1034
|
+
klass = Struct.new(:bees)
|
1035
|
+
|
1036
|
+
expect(RSpec).to receive(:warn_deprecation).with(block_regex(__LINE__ + 1))
|
1037
|
+
klass.any_instance.should_receive(:foo).with(3) { |b| :some_return_value }
|
1038
|
+
|
1039
|
+
instance = klass.new(:faces)
|
1040
|
+
instance.foo(3)
|
1041
|
+
end
|
1042
|
+
|
1043
|
+
it "works with a do end style block" do
|
1044
|
+
klass = Struct.new(:bees)
|
1045
|
+
|
1046
|
+
expect(RSpec).to receive(:warn_deprecation).with(block_regex(__LINE__ + 1))
|
1047
|
+
klass.any_instance.should_receive(:foo).with(3) do |a|
|
1048
|
+
:some_return_value
|
1049
|
+
end
|
1050
|
+
|
1051
|
+
instance = klass.new(:faces)
|
1052
|
+
instance.foo(3)
|
1053
|
+
end
|
1054
|
+
|
1055
|
+
it "won't warn if there is no implementation block on an expectation" do
|
1056
|
+
expect(RSpec).not_to receive(:warn_deprecation)
|
1057
|
+
|
1058
|
+
klass = Struct.new(:bees)
|
1059
|
+
|
1060
|
+
allow_any_instance_of(klass).to receive(:foo)
|
1061
|
+
klass.new(:faces).foo
|
1062
|
+
end
|
1063
|
+
|
1064
|
+
it "won't warn if the implementation block ignores arguments", :if => (RUBY_VERSION.to_f > 1.8) do
|
1065
|
+
expect(RSpec).not_to receive(:warn_deprecation)
|
1066
|
+
|
1067
|
+
klass = Struct.new(:bees)
|
1068
|
+
allow_any_instance_of(klass).to receive(:foo) { 5 }
|
1069
|
+
klass.new(:faces).foo
|
1070
|
+
end
|
1071
|
+
|
1072
|
+
it "warns if the implementation block accepts a splat" do
|
1073
|
+
klass = Struct.new(:bees)
|
1074
|
+
|
1075
|
+
expect(RSpec).to receive(:warn_deprecation).with(block_regex(__LINE__ + 1))
|
1076
|
+
allow_any_instance_of(klass).to receive(:foo) { |*a| 5 }
|
1077
|
+
|
1078
|
+
klass.new(:faces).foo
|
1079
|
+
end
|
1080
|
+
|
1081
|
+
it "warns if it the implementation is a lambda that expects no arguments" do
|
1082
|
+
klass = Struct.new(:bees)
|
1083
|
+
|
1084
|
+
expect(RSpec).to receive(:warn_deprecation).with(block_regex(__LINE__ + 1))
|
1085
|
+
allow_any_instance_of(klass).to receive(:foo, &lambda { 5 })
|
1086
|
+
|
1087
|
+
klass.new(:faces).foo
|
1088
|
+
end
|
1089
|
+
|
1090
|
+
it "will warn about stubs receiving blocks in 3.0" do
|
1091
|
+
klass = Struct.new(:bees)
|
1092
|
+
|
1093
|
+
expect(RSpec).to receive(:warn_deprecation).with(block_regex(__LINE__ + 1))
|
1094
|
+
expect_any_instance_of(klass).to receive(:foo) { |args| }
|
1095
|
+
klass.new(:faces).foo
|
1096
|
+
end
|
1097
|
+
|
1098
|
+
it "won't warn if there is no implementation block on an stub" do
|
1099
|
+
expect(RSpec).not_to receive(:warn_deprecation)
|
1100
|
+
|
1101
|
+
klass = Struct.new(:bees)
|
1102
|
+
|
1103
|
+
allow_any_instance_of(klass).to receive(:foo)
|
1104
|
+
klass.new(:faces).foo
|
1105
|
+
end
|
1106
|
+
end
|
1107
|
+
end
|
1108
|
+
|
959
1109
|
context 'when used in conjunction with a `dup`' do
|
960
1110
|
it "doesn't cause an infinite loop" do
|
961
1111
|
pending "This intermittently fails on JRuby" if RUBY_PLATFORM == 'java'
|
@@ -1026,7 +1176,6 @@ module RSpec
|
|
1026
1176
|
expect(instance.existing_method).to eq :stubbed_return_value
|
1027
1177
|
|
1028
1178
|
RSpec::Mocks.verify
|
1029
|
-
RSpec::Mocks.teardown
|
1030
1179
|
|
1031
1180
|
expect(instance.existing_method).to eq :existing_method_return_value
|
1032
1181
|
end
|