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.
- checksums.yaml +7 -0
- checksums.yaml.gz.sig +0 -0
- data.tar.gz.sig +0 -0
- data/Changelog.md +104 -36
- data/README.md +35 -4
- data/lib/rspec/mocks.rb +52 -13
- data/lib/rspec/mocks/any_instance.rb +10 -0
- data/lib/rspec/mocks/any_instance/chain.rb +17 -0
- data/lib/rspec/mocks/any_instance/expect_chain_chain.rb +1 -1
- data/lib/rspec/mocks/any_instance/expectation_chain.rb +9 -6
- data/lib/rspec/mocks/any_instance/message_chains.rb +10 -1
- data/lib/rspec/mocks/any_instance/proxy.rb +112 -0
- data/lib/rspec/mocks/any_instance/recorder.rb +49 -18
- data/lib/rspec/mocks/any_instance/stub_chain.rb +5 -3
- data/lib/rspec/mocks/any_instance/stub_chain_chain.rb +1 -1
- data/lib/rspec/mocks/argument_list_matcher.rb +5 -3
- data/lib/rspec/mocks/argument_matchers.rb +28 -12
- data/lib/rspec/mocks/configuration.rb +29 -0
- data/lib/rspec/mocks/error_generator.rb +2 -5
- data/lib/rspec/mocks/example_methods.rb +122 -13
- data/lib/rspec/mocks/instance_method_stasher.rb +26 -2
- data/lib/rspec/mocks/marshal_extension.rb +41 -0
- data/lib/rspec/mocks/matchers/expectation_customization.rb +20 -0
- data/lib/rspec/mocks/matchers/receive.rb +6 -20
- data/lib/rspec/mocks/matchers/receive_message_chain.rb +7 -5
- data/lib/rspec/mocks/matchers/receive_messages.rb +1 -2
- data/lib/rspec/mocks/message_chain.rb +9 -9
- data/lib/rspec/mocks/message_expectation.rb +6 -5
- data/lib/rspec/mocks/method_double.rb +67 -10
- data/lib/rspec/mocks/method_reference.rb +1 -1
- data/lib/rspec/mocks/mutate_const.rb +10 -14
- data/lib/rspec/mocks/object_reference.rb +7 -4
- data/lib/rspec/mocks/proxy.rb +95 -8
- data/lib/rspec/mocks/space.rb +58 -9
- data/lib/rspec/mocks/syntax.rb +128 -210
- data/lib/rspec/mocks/targets.rb +6 -8
- data/lib/rspec/mocks/test_double.rb +16 -25
- data/lib/rspec/mocks/verifying_double.rb +15 -12
- data/lib/rspec/mocks/verifying_message_expecation.rb +3 -3
- data/lib/rspec/mocks/verifying_proxy.rb +23 -17
- data/lib/rspec/mocks/version.rb +3 -1
- metadata +29 -257
- metadata.gz.sig +0 -0
- data/features/README.md +0 -75
- data/features/Scope.md +0 -17
- data/features/Upgrade.md +0 -22
- data/features/argument_matchers/README.md +0 -27
- data/features/argument_matchers/explicit.feature +0 -59
- data/features/argument_matchers/general_matchers.feature +0 -85
- data/features/argument_matchers/type_matchers.feature +0 -26
- data/features/message_expectations/README.md +0 -75
- data/features/message_expectations/allow_any_instance_of.feature +0 -26
- data/features/message_expectations/any_instance.feature +0 -43
- data/features/message_expectations/block_local_expectations.feature.pending +0 -55
- data/features/message_expectations/call_original.feature +0 -23
- data/features/message_expectations/expect_message_using_expect.feature +0 -107
- data/features/message_expectations/expect_message_using_should_receive.feature +0 -118
- data/features/message_expectations/message_chains_using_expect.feature +0 -49
- data/features/message_expectations/receive_counts.feature +0 -209
- data/features/message_expectations/warn_when_expectation_is_set_on_nil.feature +0 -50
- data/features/method_stubs/README.md +0 -77
- data/features/method_stubs/allow_any_instance_of.feature +0 -136
- data/features/method_stubs/as_null_object.feature +0 -40
- data/features/method_stubs/receive_message_chain.feature +0 -51
- data/features/method_stubs/simple_return_value_with_allow.feature +0 -44
- data/features/method_stubs/simple_return_value_with_stub.feature +0 -64
- data/features/method_stubs/stub_implementation.feature +0 -48
- data/features/method_stubs/to_ary.feature +0 -51
- data/features/mutating_constants/README.md +0 -82
- data/features/mutating_constants/hiding_defined_constant.feature +0 -64
- data/features/mutating_constants/stub_defined_constant.feature +0 -78
- data/features/mutating_constants/stub_undefined_constant.feature +0 -50
- data/features/outside_rspec/standalone.feature +0 -33
- data/features/spies/spy_partial_mock_method.feature +0 -34
- data/features/spies/spy_pure_mock_method.feature +0 -76
- data/features/spies/spy_unstubbed_method.feature +0 -18
- data/features/step_definitions/additional_cli_steps.rb +0 -11
- data/features/support/env.rb +0 -22
- data/features/support/rubinius.rb +0 -6
- data/features/test_frameworks/test_unit.feature +0 -54
- data/features/verifying_doubles/README.md +0 -17
- data/features/verifying_doubles/class_doubles.feature +0 -73
- data/features/verifying_doubles/dynamic_classes.feature +0 -71
- data/features/verifying_doubles/instance_doubles.feature +0 -103
- data/features/verifying_doubles/object_doubles.feature +0 -65
- data/features/verifying_doubles/partial_doubles.feature +0 -34
- data/lib/rspec/mocks/extensions/marshal.rb +0 -17
- data/lib/rspec/mocks/framework.rb +0 -35
- data/lib/rspec/mocks/method_signature_verifier.rb +0 -207
- data/lib/rspec/mocks/ruby_features.rb +0 -24
- data/spec/rspec/mocks/and_call_original_spec.rb +0 -268
- data/spec/rspec/mocks/and_return_spec.rb +0 -23
- data/spec/rspec/mocks/and_yield_spec.rb +0 -126
- data/spec/rspec/mocks/any_instance/message_chains_spec.rb +0 -41
- data/spec/rspec/mocks/any_instance_spec.rb +0 -1045
- data/spec/rspec/mocks/array_including_matcher_spec.rb +0 -41
- data/spec/rspec/mocks/at_least_spec.rb +0 -123
- data/spec/rspec/mocks/at_most_spec.rb +0 -90
- data/spec/rspec/mocks/before_all_spec.rb +0 -132
- data/spec/rspec/mocks/block_return_value_spec.rb +0 -70
- data/spec/rspec/mocks/combining_implementation_instructions_spec.rb +0 -204
- data/spec/rspec/mocks/configuration_spec.rb +0 -242
- data/spec/rspec/mocks/double_spec.rb +0 -874
- data/spec/rspec/mocks/example_methods_spec.rb +0 -13
- data/spec/rspec/mocks/extensions/marshal_spec.rb +0 -54
- data/spec/rspec/mocks/failing_argument_matchers_spec.rb +0 -184
- data/spec/rspec/mocks/hash_excluding_matcher_spec.rb +0 -67
- data/spec/rspec/mocks/hash_including_matcher_spec.rb +0 -94
- data/spec/rspec/mocks/instance_method_stasher_spec.rb +0 -74
- data/spec/rspec/mocks/matchers/have_received_spec.rb +0 -347
- data/spec/rspec/mocks/matchers/receive_message_chain_spec.rb +0 -198
- data/spec/rspec/mocks/matchers/receive_messages_spec.rb +0 -140
- data/spec/rspec/mocks/matchers/receive_spec.rb +0 -418
- data/spec/rspec/mocks/method_signature_verifier_spec.rb +0 -272
- data/spec/rspec/mocks/methods_spec.rb +0 -26
- data/spec/rspec/mocks/mock_expectation_error_spec.rb +0 -22
- data/spec/rspec/mocks/mock_ordering_spec.rb +0 -114
- data/spec/rspec/mocks/multiple_return_value_spec.rb +0 -132
- data/spec/rspec/mocks/mutate_const_spec.rb +0 -542
- data/spec/rspec/mocks/nil_expectation_warning_spec.rb +0 -52
- data/spec/rspec/mocks/null_object_mock_spec.rb +0 -133
- data/spec/rspec/mocks/once_counts_spec.rb +0 -52
- data/spec/rspec/mocks/options_hash_spec.rb +0 -35
- data/spec/rspec/mocks/order_group_spec.rb +0 -27
- data/spec/rspec/mocks/partial_double_spec.rb +0 -308
- data/spec/rspec/mocks/partial_double_using_mocks_directly_spec.rb +0 -95
- data/spec/rspec/mocks/passing_argument_matchers_spec.rb +0 -145
- data/spec/rspec/mocks/precise_counts_spec.rb +0 -68
- data/spec/rspec/mocks/record_messages_spec.rb +0 -26
- data/spec/rspec/mocks/serialization_spec.rb +0 -90
- data/spec/rspec/mocks/space_spec.rb +0 -244
- data/spec/rspec/mocks/stash_spec.rb +0 -46
- data/spec/rspec/mocks/stub_chain_spec.rb +0 -168
- data/spec/rspec/mocks/stub_implementation_spec.rb +0 -81
- data/spec/rspec/mocks/stub_spec.rb +0 -327
- data/spec/rspec/mocks/stubbed_message_expectations_spec.rb +0 -58
- data/spec/rspec/mocks/syntax_agnostic_message_matchers_spec.rb +0 -101
- data/spec/rspec/mocks/syntax_spec.rb +0 -19
- data/spec/rspec/mocks/test_double_spec.rb +0 -49
- data/spec/rspec/mocks/to_ary_spec.rb +0 -54
- data/spec/rspec/mocks/twice_counts_spec.rb +0 -66
- data/spec/rspec/mocks/verifying_double_spec.rb +0 -590
- data/spec/rspec/mocks/verifying_message_expecation_spec.rb +0 -69
- data/spec/rspec/mocks_spec.rb +0 -192
- data/spec/spec_helper.rb +0 -121
@@ -1,46 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
module RSpec
|
4
|
-
module Mocks
|
5
|
-
|
6
|
-
describe "only stashing the original method" do
|
7
|
-
let(:klass) do
|
8
|
-
Class.new do
|
9
|
-
def self.foo(arg)
|
10
|
-
:original_value
|
11
|
-
end
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
it "keeps the original method intact after multiple expectations are added on the same method" do
|
16
|
-
klass.should_receive(:foo).with(:fizbaz).and_return(:wowwow)
|
17
|
-
klass.should_receive(:foo).with(:bazbar).and_return(:okay)
|
18
|
-
|
19
|
-
klass.foo(:fizbaz)
|
20
|
-
klass.foo(:bazbar)
|
21
|
-
verify klass
|
22
|
-
|
23
|
-
reset klass
|
24
|
-
expect(klass.foo(:yeah)).to equal(:original_value)
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
describe "when a class method is aliased on a subclass and the method is mocked" do
|
29
|
-
it "restores the original aliased public method" do
|
30
|
-
klass = Class.new do
|
31
|
-
class << self
|
32
|
-
alias alternate_new new
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
klass.should_receive(:alternate_new)
|
37
|
-
expect(klass.alternate_new).to be_nil
|
38
|
-
|
39
|
-
verify klass
|
40
|
-
|
41
|
-
reset klass
|
42
|
-
expect(klass.alternate_new).to be_an_instance_of(klass)
|
43
|
-
end
|
44
|
-
end
|
45
|
-
end
|
46
|
-
end
|
@@ -1,168 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
module RSpec
|
4
|
-
module Mocks
|
5
|
-
describe "A chained method stub" do
|
6
|
-
let(:object) { Object.new }
|
7
|
-
|
8
|
-
it 'does not get confused by symbol vs string usage for the messages' do
|
9
|
-
object.stub_chain(:foo, :bar => 1)
|
10
|
-
object.stub_chain("foo", :bazz => 2)
|
11
|
-
|
12
|
-
expect(object.foo.bar).to eq(1)
|
13
|
-
expect(object.foo.bazz).to eq(2)
|
14
|
-
end
|
15
|
-
|
16
|
-
context "with one method in chain" do
|
17
|
-
context "using and_return" do
|
18
|
-
it "returns expected value from chaining only one method call" do
|
19
|
-
object.stub_chain(:msg1).and_return(:return_value)
|
20
|
-
expect(object.msg1).to equal(:return_value)
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
context "using a block" do
|
25
|
-
it "returns the correct value" do
|
26
|
-
object.stub_chain(:msg1) { :return_value }
|
27
|
-
expect(object.msg1).to equal(:return_value)
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
context "using a hash" do
|
32
|
-
it "returns the value of the key/value pair" do
|
33
|
-
object.stub_chain(:msg1 => :return_value)
|
34
|
-
expect(object.msg1).to equal(:return_value)
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
context "with two methods in chain" do
|
40
|
-
it "accepts any number of arguments to the stubbed messages in the chain" do
|
41
|
-
object.stub_chain(:msg1, :msg2).and_return(:return_value)
|
42
|
-
|
43
|
-
expect(object.msg1("nonsense", :value).msg2("another", :nonsense, 3.0, "value")).to eq(:return_value)
|
44
|
-
end
|
45
|
-
|
46
|
-
context "using and_return" do
|
47
|
-
it "returns expected value from chaining two method calls" do
|
48
|
-
object.stub_chain(:msg1, :msg2).and_return(:return_value)
|
49
|
-
expect(object.msg1.msg2).to equal(:return_value)
|
50
|
-
end
|
51
|
-
end
|
52
|
-
|
53
|
-
context "using a block" do
|
54
|
-
it "returns the correct value" do
|
55
|
-
object.stub_chain(:msg1, :msg2) { :return_value }
|
56
|
-
expect(object.msg1.msg2).to equal(:return_value)
|
57
|
-
end
|
58
|
-
end
|
59
|
-
|
60
|
-
context "using a hash" do
|
61
|
-
it "returns the value of the key/value pair" do
|
62
|
-
object.stub_chain(:msg1, :msg2 => :return_value)
|
63
|
-
expect(object.msg1.msg2).to equal(:return_value)
|
64
|
-
end
|
65
|
-
end
|
66
|
-
end
|
67
|
-
|
68
|
-
context "with four methods in chain" do
|
69
|
-
context "using and_return" do
|
70
|
-
it "returns expected value from chaining two method calls" do
|
71
|
-
object.stub_chain(:msg1, :msg2, :msg3, :msg4).and_return(:return_value)
|
72
|
-
expect(object.msg1.msg2.msg3.msg4).to equal(:return_value)
|
73
|
-
end
|
74
|
-
end
|
75
|
-
|
76
|
-
context "using a block" do
|
77
|
-
it "returns the correct value" do
|
78
|
-
object.stub_chain(:msg1, :msg2, :msg3, :msg4) { :return_value }
|
79
|
-
expect(object.msg1.msg2.msg3.msg4).to equal(:return_value)
|
80
|
-
end
|
81
|
-
end
|
82
|
-
|
83
|
-
context "using a hash" do
|
84
|
-
it "returns the value of the key/value pair" do
|
85
|
-
object.stub_chain(:msg1, :msg2, :msg3, :msg4 => :return_value)
|
86
|
-
expect(object.msg1.msg2.msg3.msg4).to equal(:return_value)
|
87
|
-
end
|
88
|
-
end
|
89
|
-
|
90
|
-
context "using a hash with a string key" do
|
91
|
-
it "returns the value of the key/value pair" do
|
92
|
-
object.stub_chain("msg1.msg2.msg3.msg4" => :return_value)
|
93
|
-
expect(object.msg1.msg2.msg3.msg4).to equal(:return_value)
|
94
|
-
end
|
95
|
-
end
|
96
|
-
end
|
97
|
-
|
98
|
-
it "returns expected value from chaining four method calls" do
|
99
|
-
object.stub_chain(:msg1, :msg2, :msg3, :msg4).and_return(:return_value)
|
100
|
-
expect(object.msg1.msg2.msg3.msg4).to equal(:return_value)
|
101
|
-
end
|
102
|
-
|
103
|
-
context "with messages shared across multiple chains" do
|
104
|
-
context "using and_return" do
|
105
|
-
context "starting with the same message" do
|
106
|
-
it "returns expected value" do
|
107
|
-
object.stub_chain(:msg1, :msg2, :msg3).and_return(:first)
|
108
|
-
object.stub_chain(:msg1, :msg2, :msg4).and_return(:second)
|
109
|
-
|
110
|
-
expect(object.msg1.msg2.msg3).to equal(:first)
|
111
|
-
expect(object.msg1.msg2.msg4).to equal(:second)
|
112
|
-
end
|
113
|
-
end
|
114
|
-
|
115
|
-
context "starting with the different messages" do
|
116
|
-
it "returns expected value" do
|
117
|
-
object.stub_chain(:msg1, :msg2, :msg3).and_return(:first)
|
118
|
-
object.stub_chain(:msg4, :msg2, :msg3).and_return(:second)
|
119
|
-
|
120
|
-
expect(object.msg1.msg2.msg3).to equal(:first)
|
121
|
-
expect(object.msg4.msg2.msg3).to equal(:second)
|
122
|
-
end
|
123
|
-
end
|
124
|
-
end
|
125
|
-
|
126
|
-
context "using => value" do
|
127
|
-
context "starting with the same message" do
|
128
|
-
it "returns expected value" do
|
129
|
-
object.stub_chain(:msg1, :msg2, :msg3 => :first)
|
130
|
-
object.stub_chain(:msg1, :msg2, :msg4 => :second)
|
131
|
-
|
132
|
-
expect(object.msg1.msg2.msg3).to equal(:first)
|
133
|
-
expect(object.msg1.msg2.msg4).to equal(:second)
|
134
|
-
end
|
135
|
-
end
|
136
|
-
|
137
|
-
context "starting with different messages" do
|
138
|
-
it "returns expected value" do
|
139
|
-
object.stub_chain(:msg1, :msg2, :msg3 => :first)
|
140
|
-
object.stub_chain(:msg4, :msg2, :msg3 => :second)
|
141
|
-
|
142
|
-
expect(object.msg1.msg2.msg3).to equal(:first)
|
143
|
-
expect(object.msg4.msg2.msg3).to equal(:second)
|
144
|
-
end
|
145
|
-
end
|
146
|
-
end
|
147
|
-
end
|
148
|
-
|
149
|
-
it "returns expected value when chain is a dot separated string, like stub_chain('msg1.msg2.msg3')" do
|
150
|
-
object.stub_chain("msg1.msg2.msg3").and_return(:return_value)
|
151
|
-
expect(object.msg1.msg2.msg3).to equal(:return_value)
|
152
|
-
end
|
153
|
-
|
154
|
-
it "returns expected value from two chains with shared messages at the beginning" do
|
155
|
-
object.stub_chain(:msg1, :msg2, :msg3, :msg4).and_return(:first)
|
156
|
-
object.stub_chain(:msg1, :msg2, :msg3, :msg5).and_return(:second)
|
157
|
-
|
158
|
-
expect(object.msg1.msg2.msg3.msg4).to equal(:first)
|
159
|
-
expect(object.msg1.msg2.msg3.msg5).to equal(:second)
|
160
|
-
end
|
161
|
-
|
162
|
-
it "handles private instance methods (like Object#select) in the middle of a chain" do
|
163
|
-
object.stub_chain(:msg1, :select, :msg3 => 'answer')
|
164
|
-
expect(object.msg1.select.msg3).to eq 'answer'
|
165
|
-
end
|
166
|
-
end
|
167
|
-
end
|
168
|
-
end
|
@@ -1,81 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
module RSpec
|
4
|
-
module Mocks
|
5
|
-
describe "stub implementation" do
|
6
|
-
describe "with no args" do
|
7
|
-
it "execs the block when called" do
|
8
|
-
obj = double()
|
9
|
-
obj.stub(:foo) { :bar }
|
10
|
-
expect(obj.foo).to eq :bar
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
14
|
-
describe "with one arg" do
|
15
|
-
it "execs the block with that arg when called" do
|
16
|
-
obj = double()
|
17
|
-
obj.stub(:foo) {|given| given}
|
18
|
-
expect(obj.foo(:bar)).to eq :bar
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
describe "with variable args" do
|
23
|
-
it "execs the block when called" do
|
24
|
-
obj = double()
|
25
|
-
obj.stub(:foo) {|*given| given.first}
|
26
|
-
expect(obj.foo(:bar)).to eq :bar
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
|
32
|
-
describe "unstub implementation" do
|
33
|
-
it "replaces the stubbed method with the original method" do
|
34
|
-
obj = Object.new
|
35
|
-
def obj.foo; :original; end
|
36
|
-
obj.stub(:foo)
|
37
|
-
obj.unstub(:foo)
|
38
|
-
expect(obj.foo).to eq :original
|
39
|
-
end
|
40
|
-
|
41
|
-
it "removes all stubs with the supplied method name" do
|
42
|
-
obj = Object.new
|
43
|
-
def obj.foo; :original; end
|
44
|
-
obj.stub(:foo).with(1)
|
45
|
-
obj.stub(:foo).with(2)
|
46
|
-
obj.unstub(:foo)
|
47
|
-
expect(obj.foo).to eq :original
|
48
|
-
end
|
49
|
-
|
50
|
-
it "does not remove any expectations with the same method name" do
|
51
|
-
obj = Object.new
|
52
|
-
def obj.foo; :original; end
|
53
|
-
obj.should_receive(:foo).with(3).and_return(:three)
|
54
|
-
obj.stub(:foo).with(1)
|
55
|
-
obj.stub(:foo).with(2)
|
56
|
-
obj.unstub(:foo)
|
57
|
-
expect(obj.foo(3)).to eq :three
|
58
|
-
end
|
59
|
-
|
60
|
-
it "restores the correct implementations when stubbed and unstubbed on a parent and child class" do
|
61
|
-
parent = Class.new
|
62
|
-
child = Class.new(parent)
|
63
|
-
|
64
|
-
parent.stub(:new)
|
65
|
-
child.stub(:new)
|
66
|
-
parent.unstub(:new)
|
67
|
-
child.unstub(:new)
|
68
|
-
|
69
|
-
expect(parent.new).to be_an_instance_of parent
|
70
|
-
expect(child.new).to be_an_instance_of child
|
71
|
-
end
|
72
|
-
|
73
|
-
it "raises a MockExpectationError if the method has not been stubbed" do
|
74
|
-
obj = Object.new
|
75
|
-
expect {
|
76
|
-
obj.unstub(:foo)
|
77
|
-
}.to raise_error(RSpec::Mocks::MockExpectationError)
|
78
|
-
end
|
79
|
-
end
|
80
|
-
end
|
81
|
-
end
|
@@ -1,327 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
module RSpec
|
4
|
-
module Mocks
|
5
|
-
describe "A method stub" do
|
6
|
-
before(:each) do
|
7
|
-
@class = Class.new do
|
8
|
-
class << self
|
9
|
-
def existing_class_method
|
10
|
-
existing_private_class_method
|
11
|
-
end
|
12
|
-
|
13
|
-
private
|
14
|
-
def existing_private_class_method
|
15
|
-
:original_value
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
def existing_instance_method
|
20
|
-
existing_private_instance_method
|
21
|
-
end
|
22
|
-
|
23
|
-
private
|
24
|
-
def existing_private_instance_method
|
25
|
-
:original_value
|
26
|
-
end
|
27
|
-
end
|
28
|
-
@instance = @class.new
|
29
|
-
@stub = Object.new
|
30
|
-
end
|
31
|
-
|
32
|
-
describe "using stub" do
|
33
|
-
it "returns declared value when message is received" do
|
34
|
-
@instance.stub(:msg).and_return(:return_value)
|
35
|
-
expect(@instance.msg).to equal(:return_value)
|
36
|
-
verify @instance
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
it "instructs an instance to respond_to the message" do
|
41
|
-
@instance.stub(:msg)
|
42
|
-
expect(@instance).to respond_to(:msg)
|
43
|
-
end
|
44
|
-
|
45
|
-
it "instructs a class object to respond_to the message" do
|
46
|
-
@class.stub(:msg)
|
47
|
-
expect(@class).to respond_to(:msg)
|
48
|
-
end
|
49
|
-
|
50
|
-
it "ignores when expected message is received with no args" do
|
51
|
-
@instance.stub(:msg)
|
52
|
-
@instance.msg
|
53
|
-
expect do
|
54
|
-
verify @instance
|
55
|
-
end.not_to raise_error
|
56
|
-
end
|
57
|
-
|
58
|
-
it "ignores when message is received with args" do
|
59
|
-
@instance.stub(:msg)
|
60
|
-
@instance.msg(:an_arg)
|
61
|
-
expect do
|
62
|
-
verify @instance
|
63
|
-
end.not_to raise_error
|
64
|
-
end
|
65
|
-
|
66
|
-
it "ignores when expected message is not received" do
|
67
|
-
@instance.stub(:msg)
|
68
|
-
expect do
|
69
|
-
verify @instance
|
70
|
-
end.not_to raise_error
|
71
|
-
end
|
72
|
-
|
73
|
-
it "handles multiple stubbed methods" do
|
74
|
-
@instance.stub(:msg1 => 1, :msg2 => 2)
|
75
|
-
expect(@instance.msg1).to eq(1)
|
76
|
-
expect(@instance.msg2).to eq(2)
|
77
|
-
end
|
78
|
-
|
79
|
-
describe "#rspec_reset" do
|
80
|
-
it "removes stubbed methods that didn't exist" do
|
81
|
-
@instance.stub(:non_existent_method)
|
82
|
-
reset @instance
|
83
|
-
expect(@instance).not_to respond_to(:non_existent_method)
|
84
|
-
end
|
85
|
-
|
86
|
-
it "restores existing instance methods" do
|
87
|
-
# See bug reports 8302 and 7805
|
88
|
-
@instance.stub(:existing_instance_method) { :stub_value }
|
89
|
-
reset @instance
|
90
|
-
expect(@instance.existing_instance_method).to eq(:original_value)
|
91
|
-
end
|
92
|
-
|
93
|
-
it "restores existing singleton methods with the appropriate context" do
|
94
|
-
klass = Class.new do
|
95
|
-
def self.say_hello
|
96
|
-
@hello if defined?(@hello)
|
97
|
-
end
|
98
|
-
end
|
99
|
-
|
100
|
-
subclass = Class.new(klass)
|
101
|
-
|
102
|
-
subclass.instance_variable_set(:@hello, "Hello")
|
103
|
-
expect(subclass.say_hello).to eq("Hello")
|
104
|
-
|
105
|
-
klass.stub(:say_hello) { "Howdy" }
|
106
|
-
expect(subclass.say_hello).to eq("Howdy")
|
107
|
-
|
108
|
-
reset klass
|
109
|
-
expect(subclass.say_hello).to eq("Hello")
|
110
|
-
end
|
111
|
-
|
112
|
-
it "restores existing private instance methods" do
|
113
|
-
# See bug reports 8302 and 7805
|
114
|
-
@instance.stub(:existing_private_instance_method) { :stub_value }
|
115
|
-
reset @instance
|
116
|
-
expect(@instance.send(:existing_private_instance_method)).to eq(:original_value)
|
117
|
-
end
|
118
|
-
|
119
|
-
it "restores existing class methods" do
|
120
|
-
# See bug reports 8302 and 7805
|
121
|
-
@class.stub(:existing_class_method) { :stub_value }
|
122
|
-
reset @class
|
123
|
-
expect(@class.existing_class_method).to eq(:original_value)
|
124
|
-
end
|
125
|
-
|
126
|
-
it "restores existing private class methods" do
|
127
|
-
# See bug reports 8302 and 7805
|
128
|
-
@class.stub(:existing_private_class_method) { :stub_value }
|
129
|
-
reset @class
|
130
|
-
expect(@class.send(:existing_private_class_method)).to eq(:original_value)
|
131
|
-
end
|
132
|
-
|
133
|
-
it "does not remove existing methods that have been stubbed twice" do
|
134
|
-
@instance.stub(:existing_instance_method)
|
135
|
-
@instance.stub(:existing_instance_method)
|
136
|
-
|
137
|
-
reset @instance
|
138
|
-
|
139
|
-
expect(@instance.existing_instance_method).to eq(:original_value)
|
140
|
-
end
|
141
|
-
|
142
|
-
it "correctly restores the visibility of methods whose visibility has been tweaked on the singleton class" do
|
143
|
-
# hello is a private method when mixed in, but public on the module
|
144
|
-
# itself
|
145
|
-
mod = Module.new {
|
146
|
-
extend self
|
147
|
-
def hello; :hello; end
|
148
|
-
|
149
|
-
private :hello
|
150
|
-
class << self; public :hello; end;
|
151
|
-
}
|
152
|
-
|
153
|
-
expect(mod.hello).to eq(:hello)
|
154
|
-
|
155
|
-
mod.stub(:hello) { :stub }
|
156
|
-
reset mod
|
157
|
-
|
158
|
-
expect(mod.hello).to eq(:hello)
|
159
|
-
end
|
160
|
-
|
161
|
-
if RUBY_VERSION >= '2.0.0'
|
162
|
-
context "with a prepended module (ruby 2.0.0+)" do
|
163
|
-
before do
|
164
|
-
mod = Module.new do
|
165
|
-
def existing_instance_method
|
166
|
-
"#{super}_prepended".to_sym
|
167
|
-
end
|
168
|
-
end
|
169
|
-
|
170
|
-
@prepended_class = Class.new(@class) do
|
171
|
-
prepend mod
|
172
|
-
|
173
|
-
def non_prepended_method
|
174
|
-
:not_prepended
|
175
|
-
end
|
176
|
-
end
|
177
|
-
@prepended_instance = @prepended_class.new
|
178
|
-
end
|
179
|
-
|
180
|
-
it "restores prepended instance methods" do
|
181
|
-
allow(@prepended_instance).to receive(:existing_instance_method) { :stubbed }
|
182
|
-
expect(@prepended_instance.existing_instance_method).to eq :stubbed
|
183
|
-
|
184
|
-
reset @prepended_instance
|
185
|
-
expect(@prepended_instance.existing_instance_method).to eq :original_value_prepended
|
186
|
-
end
|
187
|
-
|
188
|
-
it "restores non-prepended instance methods" do
|
189
|
-
allow(@prepended_instance).to receive(:non_prepended_method) { :stubbed }
|
190
|
-
expect(@prepended_instance.non_prepended_method).to eq :stubbed
|
191
|
-
|
192
|
-
reset @prepended_instance
|
193
|
-
expect(@prepended_instance.non_prepended_method).to eq :not_prepended
|
194
|
-
end
|
195
|
-
end
|
196
|
-
end
|
197
|
-
end
|
198
|
-
|
199
|
-
it "returns values in order to consecutive calls" do
|
200
|
-
@instance.stub(:msg).and_return("1",2,:three)
|
201
|
-
expect(@instance.msg).to eq("1")
|
202
|
-
expect(@instance.msg).to eq(2)
|
203
|
-
expect(@instance.msg).to eq(:three)
|
204
|
-
end
|
205
|
-
|
206
|
-
it "keeps returning last value in consecutive calls" do
|
207
|
-
@instance.stub(:msg).and_return("1",2,:three)
|
208
|
-
expect(@instance.msg).to eq("1")
|
209
|
-
expect(@instance.msg).to eq(2)
|
210
|
-
expect(@instance.msg).to eq(:three)
|
211
|
-
expect(@instance.msg).to eq(:three)
|
212
|
-
expect(@instance.msg).to eq(:three)
|
213
|
-
end
|
214
|
-
|
215
|
-
it "yields a specified object" do
|
216
|
-
@instance.stub(:method_that_yields).and_yield(:yielded_obj)
|
217
|
-
current_value = :value_before
|
218
|
-
@instance.method_that_yields {|val| current_value = val}
|
219
|
-
expect(current_value).to eq :yielded_obj
|
220
|
-
verify @instance
|
221
|
-
end
|
222
|
-
|
223
|
-
it "yields multiple times with multiple calls to and_yield" do
|
224
|
-
@instance.stub(:method_that_yields_multiple_times).and_yield(:yielded_value).
|
225
|
-
and_yield(:another_value)
|
226
|
-
current_value = []
|
227
|
-
@instance.method_that_yields_multiple_times {|val| current_value << val}
|
228
|
-
expect(current_value).to eq [:yielded_value, :another_value]
|
229
|
-
verify @instance
|
230
|
-
end
|
231
|
-
|
232
|
-
it "yields a specified object and return another specified object" do
|
233
|
-
yielded_obj = double("my mock")
|
234
|
-
yielded_obj.should_receive(:foo).with(:bar)
|
235
|
-
@instance.stub(:method_that_yields_and_returns).and_yield(yielded_obj).and_return(:baz)
|
236
|
-
expect(@instance.method_that_yields_and_returns { |o| o.foo :bar }).to eq :baz
|
237
|
-
end
|
238
|
-
|
239
|
-
it "throws when told to" do
|
240
|
-
@stub.stub(:something).and_throw(:up)
|
241
|
-
expect { @stub.something }.to throw_symbol(:up)
|
242
|
-
end
|
243
|
-
|
244
|
-
it "throws with argument when told to" do
|
245
|
-
@stub.stub(:something).and_throw(:up, 'high')
|
246
|
-
expect { @stub.something }.to throw_symbol(:up, 'high')
|
247
|
-
end
|
248
|
-
|
249
|
-
it "overrides a pre-existing method" do
|
250
|
-
@stub.stub(:existing_instance_method).and_return(:updated_stub_value)
|
251
|
-
expect(@stub.existing_instance_method).to eq :updated_stub_value
|
252
|
-
end
|
253
|
-
|
254
|
-
it "overrides a pre-existing stub" do
|
255
|
-
@stub.stub(:foo) { 'bar' }
|
256
|
-
@stub.stub(:foo) { 'baz' }
|
257
|
-
expect(@stub.foo).to eq 'baz'
|
258
|
-
end
|
259
|
-
|
260
|
-
it "allows a stub and an expectation" do
|
261
|
-
@stub.stub(:foo).with("bar")
|
262
|
-
@stub.should_receive(:foo).with("baz")
|
263
|
-
@stub.foo("bar")
|
264
|
-
@stub.foo("baz")
|
265
|
-
end
|
266
|
-
end
|
267
|
-
|
268
|
-
describe "A method stub with args" do
|
269
|
-
before(:each) do
|
270
|
-
@stub = Object.new
|
271
|
-
@stub.stub(:foo).with("bar")
|
272
|
-
end
|
273
|
-
|
274
|
-
it "does not complain if not called" do
|
275
|
-
end
|
276
|
-
|
277
|
-
it "does not complain if called with arg" do
|
278
|
-
@stub.foo("bar")
|
279
|
-
end
|
280
|
-
|
281
|
-
it "complains if called with no arg" do
|
282
|
-
expect {
|
283
|
-
@stub.foo
|
284
|
-
}.to raise_error(/received :foo with unexpected arguments/)
|
285
|
-
end
|
286
|
-
|
287
|
-
it "complains if called with other arg", :github_issue => [123,147] do
|
288
|
-
expect {
|
289
|
-
@stub.foo("other")
|
290
|
-
}.to raise_error(/received :foo with unexpected arguments.*Please stub a default value/m)
|
291
|
-
end
|
292
|
-
|
293
|
-
it "does not complain if also mocked w/ different args" do
|
294
|
-
@stub.should_receive(:foo).with("baz")
|
295
|
-
@stub.foo("bar")
|
296
|
-
@stub.foo("baz")
|
297
|
-
end
|
298
|
-
|
299
|
-
it "complains if also mocked w/ different args AND called w/ a 3rd set of args" do
|
300
|
-
@stub.should_receive(:foo).with("baz")
|
301
|
-
@stub.foo("bar")
|
302
|
-
@stub.foo("baz")
|
303
|
-
expect {
|
304
|
-
@stub.foo("other")
|
305
|
-
}.to raise_error
|
306
|
-
end
|
307
|
-
|
308
|
-
it "supports options" do
|
309
|
-
@stub.stub(:foo, :expected_from => "bar")
|
310
|
-
end
|
311
|
-
|
312
|
-
it 'uses the correct stubbed response when responding to a mock expectation' do
|
313
|
-
@stub.stub(:bar) { 15 }
|
314
|
-
@stub.stub(:bar).with(:eighteen) { 18 }
|
315
|
-
@stub.stub(:bar).with(:thirteen) { 13 }
|
316
|
-
|
317
|
-
@stub.should_receive(:bar).exactly(4).times
|
318
|
-
|
319
|
-
expect(@stub.bar(:blah)).to eq(15)
|
320
|
-
expect(@stub.bar(:thirteen)).to eq(13)
|
321
|
-
expect(@stub.bar(:eighteen)).to eq(18)
|
322
|
-
expect(@stub.bar).to eq(15)
|
323
|
-
end
|
324
|
-
end
|
325
|
-
|
326
|
-
end
|
327
|
-
end
|