rspec-mocks 3.0.0.beta2 → 3.0.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (145) hide show
  1. checksums.yaml +7 -0
  2. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +0 -0
  4. data/Changelog.md +104 -36
  5. data/README.md +35 -4
  6. data/lib/rspec/mocks.rb +52 -13
  7. data/lib/rspec/mocks/any_instance.rb +10 -0
  8. data/lib/rspec/mocks/any_instance/chain.rb +17 -0
  9. data/lib/rspec/mocks/any_instance/expect_chain_chain.rb +1 -1
  10. data/lib/rspec/mocks/any_instance/expectation_chain.rb +9 -6
  11. data/lib/rspec/mocks/any_instance/message_chains.rb +10 -1
  12. data/lib/rspec/mocks/any_instance/proxy.rb +112 -0
  13. data/lib/rspec/mocks/any_instance/recorder.rb +49 -18
  14. data/lib/rspec/mocks/any_instance/stub_chain.rb +5 -3
  15. data/lib/rspec/mocks/any_instance/stub_chain_chain.rb +1 -1
  16. data/lib/rspec/mocks/argument_list_matcher.rb +5 -3
  17. data/lib/rspec/mocks/argument_matchers.rb +28 -12
  18. data/lib/rspec/mocks/configuration.rb +29 -0
  19. data/lib/rspec/mocks/error_generator.rb +2 -5
  20. data/lib/rspec/mocks/example_methods.rb +122 -13
  21. data/lib/rspec/mocks/instance_method_stasher.rb +26 -2
  22. data/lib/rspec/mocks/marshal_extension.rb +41 -0
  23. data/lib/rspec/mocks/matchers/expectation_customization.rb +20 -0
  24. data/lib/rspec/mocks/matchers/receive.rb +6 -20
  25. data/lib/rspec/mocks/matchers/receive_message_chain.rb +7 -5
  26. data/lib/rspec/mocks/matchers/receive_messages.rb +1 -2
  27. data/lib/rspec/mocks/message_chain.rb +9 -9
  28. data/lib/rspec/mocks/message_expectation.rb +6 -5
  29. data/lib/rspec/mocks/method_double.rb +67 -10
  30. data/lib/rspec/mocks/method_reference.rb +1 -1
  31. data/lib/rspec/mocks/mutate_const.rb +10 -14
  32. data/lib/rspec/mocks/object_reference.rb +7 -4
  33. data/lib/rspec/mocks/proxy.rb +95 -8
  34. data/lib/rspec/mocks/space.rb +58 -9
  35. data/lib/rspec/mocks/syntax.rb +128 -210
  36. data/lib/rspec/mocks/targets.rb +6 -8
  37. data/lib/rspec/mocks/test_double.rb +16 -25
  38. data/lib/rspec/mocks/verifying_double.rb +15 -12
  39. data/lib/rspec/mocks/verifying_message_expecation.rb +3 -3
  40. data/lib/rspec/mocks/verifying_proxy.rb +23 -17
  41. data/lib/rspec/mocks/version.rb +3 -1
  42. metadata +29 -257
  43. metadata.gz.sig +0 -0
  44. data/features/README.md +0 -75
  45. data/features/Scope.md +0 -17
  46. data/features/Upgrade.md +0 -22
  47. data/features/argument_matchers/README.md +0 -27
  48. data/features/argument_matchers/explicit.feature +0 -59
  49. data/features/argument_matchers/general_matchers.feature +0 -85
  50. data/features/argument_matchers/type_matchers.feature +0 -26
  51. data/features/message_expectations/README.md +0 -75
  52. data/features/message_expectations/allow_any_instance_of.feature +0 -26
  53. data/features/message_expectations/any_instance.feature +0 -43
  54. data/features/message_expectations/block_local_expectations.feature.pending +0 -55
  55. data/features/message_expectations/call_original.feature +0 -23
  56. data/features/message_expectations/expect_message_using_expect.feature +0 -107
  57. data/features/message_expectations/expect_message_using_should_receive.feature +0 -118
  58. data/features/message_expectations/message_chains_using_expect.feature +0 -49
  59. data/features/message_expectations/receive_counts.feature +0 -209
  60. data/features/message_expectations/warn_when_expectation_is_set_on_nil.feature +0 -50
  61. data/features/method_stubs/README.md +0 -77
  62. data/features/method_stubs/allow_any_instance_of.feature +0 -136
  63. data/features/method_stubs/as_null_object.feature +0 -40
  64. data/features/method_stubs/receive_message_chain.feature +0 -51
  65. data/features/method_stubs/simple_return_value_with_allow.feature +0 -44
  66. data/features/method_stubs/simple_return_value_with_stub.feature +0 -64
  67. data/features/method_stubs/stub_implementation.feature +0 -48
  68. data/features/method_stubs/to_ary.feature +0 -51
  69. data/features/mutating_constants/README.md +0 -82
  70. data/features/mutating_constants/hiding_defined_constant.feature +0 -64
  71. data/features/mutating_constants/stub_defined_constant.feature +0 -78
  72. data/features/mutating_constants/stub_undefined_constant.feature +0 -50
  73. data/features/outside_rspec/standalone.feature +0 -33
  74. data/features/spies/spy_partial_mock_method.feature +0 -34
  75. data/features/spies/spy_pure_mock_method.feature +0 -76
  76. data/features/spies/spy_unstubbed_method.feature +0 -18
  77. data/features/step_definitions/additional_cli_steps.rb +0 -11
  78. data/features/support/env.rb +0 -22
  79. data/features/support/rubinius.rb +0 -6
  80. data/features/test_frameworks/test_unit.feature +0 -54
  81. data/features/verifying_doubles/README.md +0 -17
  82. data/features/verifying_doubles/class_doubles.feature +0 -73
  83. data/features/verifying_doubles/dynamic_classes.feature +0 -71
  84. data/features/verifying_doubles/instance_doubles.feature +0 -103
  85. data/features/verifying_doubles/object_doubles.feature +0 -65
  86. data/features/verifying_doubles/partial_doubles.feature +0 -34
  87. data/lib/rspec/mocks/extensions/marshal.rb +0 -17
  88. data/lib/rspec/mocks/framework.rb +0 -35
  89. data/lib/rspec/mocks/method_signature_verifier.rb +0 -207
  90. data/lib/rspec/mocks/ruby_features.rb +0 -24
  91. data/spec/rspec/mocks/and_call_original_spec.rb +0 -268
  92. data/spec/rspec/mocks/and_return_spec.rb +0 -23
  93. data/spec/rspec/mocks/and_yield_spec.rb +0 -126
  94. data/spec/rspec/mocks/any_instance/message_chains_spec.rb +0 -41
  95. data/spec/rspec/mocks/any_instance_spec.rb +0 -1045
  96. data/spec/rspec/mocks/array_including_matcher_spec.rb +0 -41
  97. data/spec/rspec/mocks/at_least_spec.rb +0 -123
  98. data/spec/rspec/mocks/at_most_spec.rb +0 -90
  99. data/spec/rspec/mocks/before_all_spec.rb +0 -132
  100. data/spec/rspec/mocks/block_return_value_spec.rb +0 -70
  101. data/spec/rspec/mocks/combining_implementation_instructions_spec.rb +0 -204
  102. data/spec/rspec/mocks/configuration_spec.rb +0 -242
  103. data/spec/rspec/mocks/double_spec.rb +0 -874
  104. data/spec/rspec/mocks/example_methods_spec.rb +0 -13
  105. data/spec/rspec/mocks/extensions/marshal_spec.rb +0 -54
  106. data/spec/rspec/mocks/failing_argument_matchers_spec.rb +0 -184
  107. data/spec/rspec/mocks/hash_excluding_matcher_spec.rb +0 -67
  108. data/spec/rspec/mocks/hash_including_matcher_spec.rb +0 -94
  109. data/spec/rspec/mocks/instance_method_stasher_spec.rb +0 -74
  110. data/spec/rspec/mocks/matchers/have_received_spec.rb +0 -347
  111. data/spec/rspec/mocks/matchers/receive_message_chain_spec.rb +0 -198
  112. data/spec/rspec/mocks/matchers/receive_messages_spec.rb +0 -140
  113. data/spec/rspec/mocks/matchers/receive_spec.rb +0 -418
  114. data/spec/rspec/mocks/method_signature_verifier_spec.rb +0 -272
  115. data/spec/rspec/mocks/methods_spec.rb +0 -26
  116. data/spec/rspec/mocks/mock_expectation_error_spec.rb +0 -22
  117. data/spec/rspec/mocks/mock_ordering_spec.rb +0 -114
  118. data/spec/rspec/mocks/multiple_return_value_spec.rb +0 -132
  119. data/spec/rspec/mocks/mutate_const_spec.rb +0 -542
  120. data/spec/rspec/mocks/nil_expectation_warning_spec.rb +0 -52
  121. data/spec/rspec/mocks/null_object_mock_spec.rb +0 -133
  122. data/spec/rspec/mocks/once_counts_spec.rb +0 -52
  123. data/spec/rspec/mocks/options_hash_spec.rb +0 -35
  124. data/spec/rspec/mocks/order_group_spec.rb +0 -27
  125. data/spec/rspec/mocks/partial_double_spec.rb +0 -308
  126. data/spec/rspec/mocks/partial_double_using_mocks_directly_spec.rb +0 -95
  127. data/spec/rspec/mocks/passing_argument_matchers_spec.rb +0 -145
  128. data/spec/rspec/mocks/precise_counts_spec.rb +0 -68
  129. data/spec/rspec/mocks/record_messages_spec.rb +0 -26
  130. data/spec/rspec/mocks/serialization_spec.rb +0 -90
  131. data/spec/rspec/mocks/space_spec.rb +0 -244
  132. data/spec/rspec/mocks/stash_spec.rb +0 -46
  133. data/spec/rspec/mocks/stub_chain_spec.rb +0 -168
  134. data/spec/rspec/mocks/stub_implementation_spec.rb +0 -81
  135. data/spec/rspec/mocks/stub_spec.rb +0 -327
  136. data/spec/rspec/mocks/stubbed_message_expectations_spec.rb +0 -58
  137. data/spec/rspec/mocks/syntax_agnostic_message_matchers_spec.rb +0 -101
  138. data/spec/rspec/mocks/syntax_spec.rb +0 -19
  139. data/spec/rspec/mocks/test_double_spec.rb +0 -49
  140. data/spec/rspec/mocks/to_ary_spec.rb +0 -54
  141. data/spec/rspec/mocks/twice_counts_spec.rb +0 -66
  142. data/spec/rspec/mocks/verifying_double_spec.rb +0 -590
  143. data/spec/rspec/mocks/verifying_message_expecation_spec.rb +0 -69
  144. data/spec/rspec/mocks_spec.rb +0 -192
  145. data/spec/spec_helper.rb +0 -121
@@ -1,52 +0,0 @@
1
- require 'spec_helper'
2
-
3
- module RSpec
4
- module Mocks
5
- describe "an expectation set on nil" do
6
- it "issues a warning with file and line number information" do
7
- expected_warning = %r%An expectation of :foo was set on nil. Called from #{__FILE__}:#{__LINE__+3}(:in .+)?. Use allow_message_expectations_on_nil to disable warnings.%
8
- Kernel.should_receive(:warn).with(expected_warning)
9
-
10
- nil.should_receive(:foo)
11
- nil.foo
12
- end
13
-
14
- it "issues a warning when the expectation is negative" do
15
- Kernel.should_receive(:warn)
16
-
17
- nil.should_not_receive(:foo)
18
- end
19
-
20
- it "does not issue a warning when expectations are set to be allowed" do
21
- allow_message_expectations_on_nil
22
- Kernel.should_not_receive(:warn)
23
-
24
- nil.should_receive(:foo)
25
- nil.should_not_receive(:bar)
26
- nil.foo
27
- end
28
-
29
- it 'does not call #nil? on a double extra times' do
30
- dbl = double
31
- dbl.should_receive(:nil?).once.and_return(false)
32
- dbl.nil?
33
- end
34
- end
35
-
36
- describe "#allow_message_expectations_on_nil" do
37
- it "does not affect subsequent examples" do
38
- allow_message_expectations_on_nil
39
- RSpec::Mocks.teardown
40
- RSpec::Mocks.setup
41
- Kernel.should_receive(:warn)
42
- nil.should_receive(:foo)
43
- nil.foo
44
- end
45
-
46
- it 'doesnt error when marshalled' do
47
- allow_message_expectations_on_nil
48
- expect(Marshal.dump(nil)).to eq Marshal.dump_without_mocks(nil)
49
- end
50
- end
51
- end
52
- end
@@ -1,133 +0,0 @@
1
- require 'spec_helper'
2
-
3
- module RSpec
4
- module Mocks
5
- describe "a double _not_ acting as a null object" do
6
- before(:each) do
7
- @double = double('non-null object')
8
- end
9
-
10
- it "says it does not respond to messages it doesn't understand" do
11
- expect(@double).not_to respond_to(:foo)
12
- end
13
-
14
- it "says it responds to messages it does understand" do
15
- @double.stub(:foo)
16
- expect(@double).to respond_to(:foo)
17
- end
18
-
19
- it "raises an error when interpolated in a string as an integer" do
20
- # Not sure why, but 1.9.2 (but not JRuby --1.9) raises a different
21
- # error than 1.8.7 and 1.9.3...
22
- expected_error = (RUBY_VERSION == '1.9.2' && RUBY_PLATFORM !~ /java/) ?
23
- RSpec::Mocks::MockExpectationError :
24
- TypeError
25
-
26
- expect { "%i" % @double }.to raise_error(expected_error)
27
- end
28
- end
29
-
30
- describe "a double acting as a null object" do
31
- before(:each) do
32
- @double = double('null object').as_null_object
33
- end
34
-
35
- it "says it responds to everything" do
36
- expect(@double).to respond_to(:any_message_it_gets)
37
- end
38
-
39
- it "allows explicit stubs" do
40
- @double.stub(:foo) { "bar" }
41
- expect(@double.foo).to eq("bar")
42
- end
43
-
44
- it "allows explicit expectation" do
45
- @double.should_receive(:something)
46
- @double.something
47
- end
48
-
49
- it 'continues to return self from an explicit expectation' do
50
- @double.should_receive(:bar)
51
- expect(@double.foo.bar).to be(@double)
52
- end
53
-
54
- it 'returns an explicitly stubbed value from an expectation with no implementation' do
55
- @double.stub(:foo => "bar")
56
- @double.should_receive(:foo)
57
- expect(@double.foo).to eq("bar")
58
- end
59
-
60
- it "fails verification when explicit exception not met" do
61
- expect {
62
- @double.should_receive(:something)
63
- verify @double
64
- }.to raise_error(RSpec::Mocks::MockExpectationError)
65
- end
66
-
67
- it "ignores unexpected methods" do
68
- @double.random_call("a", "d", "c")
69
- verify @double
70
- end
71
-
72
- it 'allows unexpected message sends using `send`' do
73
- val = @double.send(:foo).send(:bar)
74
- expect(val).to equal(@double)
75
- end
76
-
77
- it 'allows unexpected message sends using `send`' do
78
- val = @double.__send__(:foo).__send__(:bar)
79
- expect(val).to equal(@double)
80
- end
81
-
82
- it "allows expected message with different args first" do
83
- @double.should_receive(:message).with(:expected_arg)
84
- @double.message(:unexpected_arg)
85
- @double.message(:expected_arg)
86
- end
87
-
88
- it "allows expected message with different args second" do
89
- @double.should_receive(:message).with(:expected_arg)
90
- @double.message(:expected_arg)
91
- @double.message(:unexpected_arg)
92
- end
93
-
94
- it "can be interpolated in a string as an integer" do
95
- # This form of string interpolation calls
96
- # @double.to_int.to_int.to_int...etc until it gets an integer,
97
- # and thus gets stuck in an infinite loop unless our double
98
- # returns an int value from #to_int.
99
- expect(("%i" % @double)).to eq("0")
100
- end
101
-
102
- it "does not allow null objects to be used outside of examples" do
103
- RSpec::Mocks.teardown
104
-
105
- expect { @double.some.long.message.chain }.to raise_error(RSpec::Mocks::OutsideOfExampleError)
106
- expect { @double.as_null_object }.to raise_error(RSpec::Mocks::OutsideOfExampleError)
107
- end
108
- end
109
-
110
- describe "#as_null_object" do
111
- it "sets the object to null_object" do
112
- obj = double('anything').as_null_object
113
- expect(obj).to be_null_object
114
- end
115
- end
116
-
117
- describe "#null_object?" do
118
- it "defaults to false" do
119
- obj = double('anything')
120
- expect(obj).not_to be_null_object
121
- end
122
- end
123
-
124
- describe "when using the :expect syntax" do
125
- include_context "with syntax", :expect
126
-
127
- it 'still supports null object doubles' do
128
- obj = double("foo").as_null_object
129
- expect(obj.foo.bar.bazz).to be(obj)
130
- end
131
- end
132
- end
133
- end
@@ -1,52 +0,0 @@
1
- require 'spec_helper'
2
-
3
- module RSpec
4
- module Mocks
5
- describe "#once" do
6
- before(:each) do
7
- @double = double
8
- end
9
-
10
- it "passes when called once" do
11
- @double.should_receive(:do_something).once
12
- @double.do_something
13
- verify @double
14
- end
15
-
16
- it "passes when called once with specified args" do
17
- @double.should_receive(:do_something).once.with("a", "b", "c")
18
- @double.do_something("a", "b", "c")
19
- verify @double
20
- end
21
-
22
- it "passes when called once with unspecified args" do
23
- @double.should_receive(:do_something).once
24
- @double.do_something("a", "b", "c")
25
- verify @double
26
- end
27
-
28
- it "fails when called with wrong args" do
29
- @double.should_receive(:do_something).once.with("a", "b", "c")
30
- expect {
31
- @double.do_something("d", "e", "f")
32
- }.to raise_error(RSpec::Mocks::MockExpectationError)
33
- reset @double
34
- end
35
-
36
- it "fails fast when called twice" do
37
- @double.should_receive(:do_something).once
38
- @double.do_something
39
- expect {
40
- @double.do_something
41
- }.to raise_error(RSpec::Mocks::MockExpectationError)
42
- end
43
-
44
- it "fails when not called" do
45
- @double.should_receive(:do_something).once
46
- expect {
47
- verify @double
48
- }.to raise_error(RSpec::Mocks::MockExpectationError)
49
- end
50
- end
51
- end
52
- end
@@ -1,35 +0,0 @@
1
- require 'spec_helper'
2
-
3
- module RSpec
4
- module Mocks
5
- describe "calling :should_receive with an options hash" do
6
- it "reports the file and line submitted with :expected_from" do
7
- begin
8
- mock = RSpec::Mocks::Double.new("a mock")
9
- mock.should_receive(:message, :expected_from => "/path/to/blah.ext:37")
10
- verify mock
11
- rescue Exception => e
12
- ensure
13
- expect(e.backtrace.to_s).to match(/\/path\/to\/blah.ext:37/m)
14
- end
15
- end
16
-
17
- it "uses the message supplied with :message" do
18
- expect {
19
- m = RSpec::Mocks::Double.new("a mock")
20
- m.should_receive(:message, :message => "recebi nada")
21
- verify m
22
- }.to raise_error("recebi nada")
23
- end
24
-
25
- it "uses the message supplied with :message after a similar stub" do
26
- expect {
27
- m = RSpec::Mocks::Double.new("a mock")
28
- m.stub(:message)
29
- m.should_receive(:message, :message => "from mock")
30
- verify m
31
- }.to raise_error("from mock")
32
- end
33
- end
34
- end
35
- end
@@ -1,27 +0,0 @@
1
- require 'spec_helper'
2
- describe 'OrderGroup' do
3
- let(:order_group) { ::RSpec::Mocks::OrderGroup.new }
4
-
5
- describe '#consume' do
6
- let(:ordered_1) { double :ordered? => true }
7
- let(:ordered_2) { double :ordered? => true }
8
- let(:unordered) { double :ordered? => false }
9
-
10
- before do
11
- order_group.register unordered
12
- order_group.register ordered_1
13
- order_group.register unordered
14
- order_group.register ordered_2
15
- order_group.register unordered
16
- order_group.register unordered
17
- end
18
-
19
- it 'returns the first ordered? expectation' do
20
- expect(order_group.consume).to eq ordered_1
21
- end
22
- it 'keeps returning ordered? expectation until all are returned' do
23
- expectations = 3.times.map { order_group.consume }
24
- expect(expectations).to eq [ordered_1, ordered_2, nil]
25
- end
26
- end
27
- end
@@ -1,308 +0,0 @@
1
- require 'spec_helper'
2
-
3
- module RSpec
4
- module Mocks
5
- describe "A partial double" do
6
- let(:object) { Object.new }
7
-
8
- it "names the class in the failure message" do
9
- object.should_receive(:foo)
10
- expect do
11
- verify object
12
- end.to raise_error(RSpec::Mocks::MockExpectationError, /\(#<Object:.*>\).foo/)
13
- end
14
-
15
- it "names the class in the failure message when expectation is on class" do
16
- Object.should_receive(:foo)
17
- expect {
18
- verify Object
19
- }.to raise_error(RSpec::Mocks::MockExpectationError, /<Object \(class\)>/)
20
- end
21
-
22
- it "does not conflict with @options in the object" do
23
- object.instance_exec { @options = Object.new }
24
- object.should_receive(:blah)
25
- object.blah
26
- end
27
-
28
- it "should_not_receive mocks out the method" do
29
- object.should_not_receive(:fuhbar)
30
- expect {
31
- object.fuhbar
32
- }.to raise_error(
33
- RSpec::Mocks::MockExpectationError,
34
- /expected\: 0 times with any arguments\n received\: 1 time/
35
- )
36
- end
37
-
38
- it "should_not_receive returns a negative message expectation" do
39
- expect(object.should_not_receive(:foobar)).to be_negative
40
- end
41
-
42
- it "should_receive mocks out the method" do
43
- object.should_receive(:foobar).with(:test_param).and_return(1)
44
- expect(object.foobar(:test_param)).to equal(1)
45
- end
46
-
47
- it "should_receive handles a hash" do
48
- object.should_receive(:foobar).with(:key => "value").and_return(1)
49
- expect(object.foobar(:key => "value")).to equal(1)
50
- end
51
-
52
- it "should_receive handles an inner hash" do
53
- hash = {:a => {:key => "value"}}
54
- object.should_receive(:foobar).with(:key => "value").and_return(1)
55
- expect(object.foobar(hash[:a])).to equal(1)
56
- end
57
-
58
- it "should_receive returns a positive message expectation" do
59
- expect(object.should_receive(:foobar)).not_to be_negative
60
- object.foobar
61
- end
62
-
63
- it "should_receive verifies method was called" do
64
- object.should_receive(:foobar).with(:test_param).and_return(1)
65
- expect {
66
- verify object
67
- }.to raise_error(RSpec::Mocks::MockExpectationError)
68
- end
69
-
70
- it "should_receive also takes a String argument" do
71
- object.should_receive('foobar')
72
- object.foobar
73
- end
74
-
75
- it "should_not_receive also takes a String argument" do
76
- object.should_not_receive('foobar')
77
- expect {
78
- object.foobar
79
- }.to raise_error(RSpec::Mocks::MockExpectationError)
80
- end
81
-
82
- it "uses reports nil in the error message" do
83
- allow_message_expectations_on_nil
84
-
85
- _nil = nil
86
- _nil.should_receive(:foobar)
87
- expect {
88
- verify _nil
89
- }.to raise_error(
90
- RSpec::Mocks::MockExpectationError,
91
- %Q|(nil).foobar(any args)\n expected: 1 time with any arguments\n received: 0 times with any arguments|
92
- )
93
- end
94
-
95
- it "includes the class name in the error when mocking a class method that is called an extra time with the wrong args" do
96
- klass = Class.new do
97
- def self.inspect
98
- "MyClass"
99
- end
100
- end
101
-
102
- klass.should_receive(:bar).with(1)
103
- klass.bar(1)
104
-
105
- expect {
106
- klass.bar(2)
107
- }.to raise_error(RSpec::Mocks::MockExpectationError, /MyClass/)
108
- end
109
- end
110
-
111
- describe "Using a partial mock on a proxy object", :if => defined?(::BasicObject) do
112
- let(:proxy_class) do
113
- Class.new(::BasicObject) do
114
- def initialize(target)
115
- @target = target
116
- end
117
-
118
- def proxied?
119
- true
120
- end
121
-
122
- def method_missing(*a)
123
- @target.send(*a)
124
- end
125
- end
126
- end
127
-
128
- let(:wrapped_object) { Object.new }
129
- let(:proxy) { proxy_class.new(wrapped_object) }
130
-
131
- it 'works properly' do
132
- proxy.should_receive(:proxied?).and_return(false)
133
- expect(proxy).not_to be_proxied
134
- end
135
-
136
- it 'does not confuse the proxy and the proxied object' do
137
- proxy.stub(:foo).and_return(:proxy_foo)
138
- wrapped_object.stub(:foo).and_return(:wrapped_foo)
139
-
140
- expect(proxy.foo).to eq(:proxy_foo)
141
- expect(wrapped_object.foo).to eq(:wrapped_foo)
142
- end
143
- end
144
-
145
- describe "Partially mocking an object that defines ==, after another mock has been defined" do
146
- before(:each) do
147
- double("existing mock", :foo => :foo)
148
- end
149
-
150
- let(:klass) do
151
- Class.new do
152
- attr_reader :val
153
- def initialize(val)
154
- @val = val
155
- end
156
-
157
- def ==(other)
158
- @val == other.val
159
- end
160
- end
161
- end
162
-
163
- it "does not raise an error when stubbing the object" do
164
- o = klass.new :foo
165
- expect { o.stub(:bar) }.not_to raise_error
166
- end
167
- end
168
-
169
- describe "A partial class mock that has been subclassed" do
170
-
171
- let(:klass) { Class.new }
172
- let(:subklass) { Class.new(klass) }
173
-
174
- it "cleans up stubs during #reset to prevent leakage onto subclasses between examples" do
175
- allow(klass).to receive(:new).and_return(:new_foo)
176
- expect(subklass.new).to eq :new_foo
177
-
178
- reset(klass)
179
-
180
- expect(subklass.new).to be_a(subklass)
181
- end
182
-
183
- describe "stubbing a base class class method" do
184
- before do
185
- klass.stub(:find).and_return "stubbed_value"
186
- end
187
-
188
- it "returns the value for the stub on the base class" do
189
- expect(klass.find).to eq "stubbed_value"
190
- end
191
-
192
- it "returns the value for the descendent class" do
193
- expect(subklass.find).to eq "stubbed_value"
194
- end
195
- end
196
- end
197
-
198
- describe "Method visibility when using partial mocks" do
199
- let(:klass) do
200
- Class.new do
201
- def public_method
202
- private_method
203
- protected_method
204
- end
205
- protected
206
- def protected_method; end
207
- private
208
- def private_method; end
209
- end
210
- end
211
-
212
- let(:object) { klass.new }
213
-
214
- it 'keeps public methods public' do
215
- object.should_receive(:public_method)
216
- expect(object.public_methods).to include_method(:public_method)
217
- object.public_method
218
- end
219
-
220
- it 'keeps private methods private' do
221
- object.should_receive(:private_method)
222
- expect(object.private_methods).to include_method(:private_method)
223
- object.public_method
224
- end
225
-
226
- it 'keeps protected methods protected' do
227
- object.should_receive(:protected_method)
228
- expect(object.protected_methods).to include_method(:protected_method)
229
- object.public_method
230
- end
231
-
232
- end
233
-
234
- describe 'when verify_partial_doubles configuration option is set' do
235
- include_context "with isolated configuration"
236
-
237
- let(:klass) do
238
- Class.new do
239
- def implemented
240
- "works"
241
- end
242
-
243
- def respond_to?(method_name, include_all=false)
244
- method_name.to_s == "dynamic_method" || super
245
- end
246
-
247
- def method_missing(method_name, *args)
248
- if respond_to?(method_name)
249
- method_name
250
- else
251
- super
252
- end
253
- end
254
- end
255
- end
256
-
257
- let(:object) { klass.new }
258
-
259
- before do
260
- RSpec::Mocks.configuration.verify_partial_doubles = true
261
- end
262
-
263
- it 'allows valid methods to be expected' do
264
- expect(object).to receive(:implemented).and_call_original
265
- expect(object.implemented).to eq("works")
266
- end
267
-
268
- it 'does not allow a non-existing method to be expected' do
269
- prevents { expect(object).to receive(:unimplemented) }
270
- end
271
-
272
- it 'verifies arity range when matching arguments' do
273
- prevents { expect(object).to receive(:implemented).with('bogus') }
274
- end
275
-
276
- it 'allows a method defined with method_missing to be expected' do
277
- expect(object).to receive(:dynamic_method).with('a').and_call_original
278
- expect(object.dynamic_method('a')).to eq(:dynamic_method)
279
- end
280
-
281
- it 'allows valid methods to be expected on any_instance' do
282
- expect_any_instance_of(klass).to receive(:implemented)
283
- object.implemented
284
- end
285
-
286
- it 'does not allow a non-existing method to be called on any_instance' do
287
- prevents { expect_any_instance_of(klass).to receive(:unimplemented) }
288
- end
289
-
290
- it 'does not allow missing methods to be called on any_instance' do
291
- # This is potentially surprising behaviour, but there is no way for us
292
- # to know that this method is valid since we only have class and not an
293
- # instance.
294
- prevents { expect_any_instance_of(klass).to receive(:dynamic_method) }
295
- end
296
-
297
- it 'verifies arity range when receiving a message' do
298
- allow(object).to receive(:implemented)
299
- expect {
300
- object.implemented('bogus')
301
- }.to raise_error(
302
- ArgumentError,
303
- a_string_including("Wrong number of arguments. Expected 0, got 1.")
304
- )
305
- end
306
- end
307
- end
308
- end