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,542 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
TOP_LEVEL_VALUE_CONST = 7
|
4
|
-
|
5
|
-
class TestClass
|
6
|
-
M = :m
|
7
|
-
N = :n
|
8
|
-
|
9
|
-
class Nested
|
10
|
-
class NestedEvenMore
|
11
|
-
end
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
class TestClassThatDefinesSend
|
16
|
-
C = :c
|
17
|
-
|
18
|
-
def self.send
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
class TestSubClass < TestClass
|
23
|
-
P = :p
|
24
|
-
end
|
25
|
-
|
26
|
-
module RSpec
|
27
|
-
module Mocks
|
28
|
-
describe "Constant Mutating" do
|
29
|
-
include RSpec::Mocks::RecursiveConstMethods
|
30
|
-
|
31
|
-
def reset_rspec_mocks
|
32
|
-
::RSpec::Mocks.space.reset_all
|
33
|
-
end
|
34
|
-
|
35
|
-
shared_context "constant example methods" do |const_name|
|
36
|
-
define_method :const do
|
37
|
-
recursive_const_get(const_name)
|
38
|
-
end
|
39
|
-
|
40
|
-
define_method :parent_const do
|
41
|
-
recursive_const_get("Object::" + const_name.sub(/(::)?[^:]+\z/, ''))
|
42
|
-
end
|
43
|
-
|
44
|
-
define_method :last_const_part do
|
45
|
-
const_name.split('::').last
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
49
|
-
shared_examples_for "loaded constant stubbing" do |const_name|
|
50
|
-
include_context "constant example methods", const_name
|
51
|
-
|
52
|
-
let!(:original_const_value) { const }
|
53
|
-
after { change_const_value_to(original_const_value) }
|
54
|
-
|
55
|
-
def change_const_value_to(value)
|
56
|
-
parent_const.__send__(:remove_const, last_const_part)
|
57
|
-
parent_const.const_set(last_const_part, value)
|
58
|
-
end
|
59
|
-
|
60
|
-
it 'allows it to be stubbed' do
|
61
|
-
expect(const).not_to eq(7)
|
62
|
-
stub_const(const_name, 7)
|
63
|
-
expect(const).to eq(7)
|
64
|
-
end
|
65
|
-
|
66
|
-
it 'resets it to its original value when rspec clears its mocks' do
|
67
|
-
original_value = const
|
68
|
-
expect(original_value).not_to eq(:a)
|
69
|
-
stub_const(const_name, :a)
|
70
|
-
reset_rspec_mocks
|
71
|
-
expect(const).to be(original_value)
|
72
|
-
end
|
73
|
-
|
74
|
-
it 'returns the stubbed value' do
|
75
|
-
expect(stub_const(const_name, 7)).to eq(7)
|
76
|
-
end
|
77
|
-
end
|
78
|
-
|
79
|
-
shared_examples_for "loaded constant hiding" do |const_name|
|
80
|
-
before do
|
81
|
-
expect(recursive_const_defined?(const_name)).to be_truthy
|
82
|
-
end
|
83
|
-
|
84
|
-
it 'allows it to be hidden' do
|
85
|
-
hide_const(const_name)
|
86
|
-
expect(recursive_const_defined?(const_name)).to be_falsey
|
87
|
-
end
|
88
|
-
|
89
|
-
it 'resets the constant when rspec clear its mocks' do
|
90
|
-
hide_const(const_name)
|
91
|
-
reset_rspec_mocks
|
92
|
-
expect(recursive_const_defined?(const_name)).to be_truthy
|
93
|
-
end
|
94
|
-
|
95
|
-
it 'returns nil' do
|
96
|
-
expect(hide_const(const_name)).to be_nil
|
97
|
-
end
|
98
|
-
end
|
99
|
-
|
100
|
-
shared_examples_for "unloaded constant stubbing" do |const_name|
|
101
|
-
include_context "constant example methods", const_name
|
102
|
-
|
103
|
-
before do
|
104
|
-
expect(recursive_const_defined?(const_name)).to be_falsey
|
105
|
-
end
|
106
|
-
|
107
|
-
it 'allows it to be stubbed' do
|
108
|
-
stub_const(const_name, 7)
|
109
|
-
expect(const).to eq(7)
|
110
|
-
end
|
111
|
-
|
112
|
-
it 'removes the constant when rspec clears its mocks' do
|
113
|
-
stub_const(const_name, 7)
|
114
|
-
reset_rspec_mocks
|
115
|
-
expect(recursive_const_defined?(const_name)).to be_falsey
|
116
|
-
end
|
117
|
-
|
118
|
-
it 'returns the stubbed value' do
|
119
|
-
expect(stub_const(const_name, 7)).to eq(7)
|
120
|
-
end
|
121
|
-
|
122
|
-
it 'ignores the :transfer_nested_constants option if passed' do
|
123
|
-
stub = Module.new
|
124
|
-
stub_const(const_name, stub, :transfer_nested_constants => true)
|
125
|
-
expect(stub.constants).to eq([])
|
126
|
-
end
|
127
|
-
end
|
128
|
-
|
129
|
-
shared_examples_for "unloaded constant hiding" do |const_name|
|
130
|
-
include_context "constant example methods", const_name
|
131
|
-
|
132
|
-
before do
|
133
|
-
expect(recursive_const_defined?(const_name)).to be_falsey
|
134
|
-
end
|
135
|
-
|
136
|
-
it 'allows it to be hidden, though the operation has no effect' do
|
137
|
-
hide_const(const_name)
|
138
|
-
expect(recursive_const_defined?(const_name)).to be_falsey
|
139
|
-
end
|
140
|
-
|
141
|
-
it 'remains undefined after rspec clears its mocks' do
|
142
|
-
hide_const(const_name)
|
143
|
-
reset_rspec_mocks
|
144
|
-
expect(recursive_const_defined?(const_name)).to be_falsey
|
145
|
-
end
|
146
|
-
|
147
|
-
it 'returns nil' do
|
148
|
-
expect(hide_const(const_name)).to be_nil
|
149
|
-
end
|
150
|
-
end
|
151
|
-
|
152
|
-
describe "#hide_const" do
|
153
|
-
context "for a loaded constant nested in a module that redefines `send`" do
|
154
|
-
it_behaves_like "loaded constant hiding", "TestClassThatDefinesSend::C"
|
155
|
-
end
|
156
|
-
|
157
|
-
|
158
|
-
context 'for a loaded nested constant' do
|
159
|
-
it_behaves_like "loaded constant hiding", "TestClass::Nested"
|
160
|
-
end
|
161
|
-
|
162
|
-
context 'for a loaded constant prefixed with ::' do
|
163
|
-
it_behaves_like 'loaded constant hiding', "::TestClass"
|
164
|
-
end
|
165
|
-
|
166
|
-
context 'for an unloaded constant with nested name that matches a top-level constant' do
|
167
|
-
it_behaves_like "unloaded constant hiding", "TestClass::Hash"
|
168
|
-
|
169
|
-
it 'does not hide the top-level constant' do
|
170
|
-
top_level_hash = ::Hash
|
171
|
-
|
172
|
-
hide_const("TestClass::Hash")
|
173
|
-
expect(::Hash).to equal(top_level_hash)
|
174
|
-
end
|
175
|
-
|
176
|
-
it 'does not affect the ability to access the top-level constant from nested contexts', :silence_warnings do
|
177
|
-
top_level_hash = ::Hash
|
178
|
-
|
179
|
-
hide_const("TestClass::Hash")
|
180
|
-
expect(TestClass::Hash).to equal(top_level_hash)
|
181
|
-
end
|
182
|
-
end
|
183
|
-
|
184
|
-
context 'for a loaded deeply nested constant' do
|
185
|
-
it_behaves_like "loaded constant hiding", "TestClass::Nested::NestedEvenMore"
|
186
|
-
end
|
187
|
-
|
188
|
-
context 'for an unloaded unnested constant' do
|
189
|
-
it_behaves_like "unloaded constant hiding", "X"
|
190
|
-
end
|
191
|
-
|
192
|
-
context 'for an unloaded nested constant' do
|
193
|
-
it_behaves_like "unloaded constant hiding", "X::Y"
|
194
|
-
end
|
195
|
-
|
196
|
-
it 'can be hidden multiple times but still restores the original value properly' do
|
197
|
-
orig_value = TestClass
|
198
|
-
hide_const("TestClass")
|
199
|
-
hide_const("TestClass")
|
200
|
-
|
201
|
-
reset_rspec_mocks
|
202
|
-
expect(TestClass).to be(orig_value)
|
203
|
-
end
|
204
|
-
|
205
|
-
it 'allows a constant to be hidden, then stubbed, restoring it to its original value properly' do
|
206
|
-
orig_value = TOP_LEVEL_VALUE_CONST
|
207
|
-
|
208
|
-
hide_const("TOP_LEVEL_VALUE_CONST")
|
209
|
-
expect(recursive_const_defined?("TOP_LEVEL_VALUE_CONST")).to be_falsey
|
210
|
-
|
211
|
-
stub_const("TOP_LEVEL_VALUE_CONST", 12345)
|
212
|
-
expect(TOP_LEVEL_VALUE_CONST).to eq 12345
|
213
|
-
|
214
|
-
reset_rspec_mocks
|
215
|
-
expect(TOP_LEVEL_VALUE_CONST).to eq orig_value
|
216
|
-
end
|
217
|
-
end
|
218
|
-
|
219
|
-
describe "#stub_const" do
|
220
|
-
context "for a loaded constant nested in a module that redefines `send`" do
|
221
|
-
it_behaves_like "loaded constant stubbing", "TestClassThatDefinesSend::C"
|
222
|
-
end
|
223
|
-
|
224
|
-
context 'for a loaded unnested constant' do
|
225
|
-
it_behaves_like "loaded constant stubbing", "TestClass"
|
226
|
-
|
227
|
-
it 'can be stubbed multiple times but still restores the original value properly' do
|
228
|
-
orig_value = TestClass
|
229
|
-
stub1, stub2 = Module.new, Module.new
|
230
|
-
stub_const("TestClass", stub1)
|
231
|
-
stub_const("TestClass", stub2)
|
232
|
-
|
233
|
-
reset_rspec_mocks
|
234
|
-
expect(TestClass).to be(orig_value)
|
235
|
-
end
|
236
|
-
|
237
|
-
it 'allows nested constants to be transferred to a stub module' do
|
238
|
-
tc_nested = TestClass::Nested
|
239
|
-
stub = Module.new
|
240
|
-
stub_const("TestClass", stub, :transfer_nested_constants => true)
|
241
|
-
expect(stub::M).to eq(:m)
|
242
|
-
expect(stub::N).to eq(:n)
|
243
|
-
expect(stub::Nested).to be(tc_nested)
|
244
|
-
end
|
245
|
-
|
246
|
-
it 'does not transfer nested constants that are inherited from a superclass' do
|
247
|
-
stub = Module.new
|
248
|
-
stub_const("TestSubClass", stub, :transfer_nested_constants => true)
|
249
|
-
expect(stub::P).to eq(:p)
|
250
|
-
expect(defined?(stub::M)).to be_falsey
|
251
|
-
expect(defined?(stub::N)).to be_falsey
|
252
|
-
end
|
253
|
-
|
254
|
-
it 'raises an error when asked to transfer a nested inherited constant' do
|
255
|
-
original_tsc = TestSubClass
|
256
|
-
|
257
|
-
expect {
|
258
|
-
stub_const("TestSubClass", Module.new, :transfer_nested_constants => [:M])
|
259
|
-
}.to raise_error(ArgumentError)
|
260
|
-
|
261
|
-
expect(TestSubClass).to be(original_tsc)
|
262
|
-
end
|
263
|
-
|
264
|
-
it 'allows nested constants to be selectively transferred to a stub module' do
|
265
|
-
stub = Module.new
|
266
|
-
stub_const("TestClass", stub, :transfer_nested_constants => [:M, :N])
|
267
|
-
expect(stub::M).to eq(:m)
|
268
|
-
expect(stub::N).to eq(:n)
|
269
|
-
expect(defined?(stub::Nested)).to be_falsey
|
270
|
-
end
|
271
|
-
|
272
|
-
it 'raises an error if asked to transfer nested constants but given an object that does not support them' do
|
273
|
-
original_tc = TestClass
|
274
|
-
stub = Object.new
|
275
|
-
expect {
|
276
|
-
stub_const("TestClass", stub, :transfer_nested_constants => true)
|
277
|
-
}.to raise_error(ArgumentError)
|
278
|
-
|
279
|
-
expect(TestClass).to be(original_tc)
|
280
|
-
|
281
|
-
expect {
|
282
|
-
stub_const("TestClass", stub, :transfer_nested_constants => [:M])
|
283
|
-
}.to raise_error(ArgumentError)
|
284
|
-
|
285
|
-
expect(TestClass).to be(original_tc)
|
286
|
-
end
|
287
|
-
|
288
|
-
it 'raises an error if asked to transfer nested constants on a constant that does not support nested constants' do
|
289
|
-
stub = Module.new
|
290
|
-
expect {
|
291
|
-
stub_const("TOP_LEVEL_VALUE_CONST", stub, :transfer_nested_constants => true)
|
292
|
-
}.to raise_error(ArgumentError)
|
293
|
-
|
294
|
-
expect(TOP_LEVEL_VALUE_CONST).to eq(7)
|
295
|
-
|
296
|
-
expect {
|
297
|
-
stub_const("TOP_LEVEL_VALUE_CONST", stub, :transfer_nested_constants => [:M])
|
298
|
-
}.to raise_error(ArgumentError)
|
299
|
-
|
300
|
-
expect(TOP_LEVEL_VALUE_CONST).to eq(7)
|
301
|
-
end
|
302
|
-
|
303
|
-
it 'raises an error if asked to transfer a nested constant that is not defined' do
|
304
|
-
original_tc = TestClass
|
305
|
-
expect(defined?(TestClass::V)).to be_falsey
|
306
|
-
stub = Module.new
|
307
|
-
|
308
|
-
expect {
|
309
|
-
stub_const("TestClass", stub, :transfer_nested_constants => [:V])
|
310
|
-
}.to raise_error(/cannot transfer nested constant.*V/i)
|
311
|
-
|
312
|
-
expect(TestClass).to be(original_tc)
|
313
|
-
end
|
314
|
-
|
315
|
-
describe 'with global transfer_nested_constant option set' do
|
316
|
-
include_context "with isolated configuration"
|
317
|
-
|
318
|
-
before do
|
319
|
-
RSpec::Mocks.configuration.transfer_nested_constants = true
|
320
|
-
end
|
321
|
-
|
322
|
-
it 'allows nested constants to be transferred to a stub module' do
|
323
|
-
tc_nested = TestClass::Nested
|
324
|
-
stub = Module.new
|
325
|
-
stub_const("TestClass", stub)
|
326
|
-
expect(stub::M).to eq(:m)
|
327
|
-
expect(stub::N).to eq(:n)
|
328
|
-
expect(stub::Nested).to be(tc_nested)
|
329
|
-
end
|
330
|
-
end
|
331
|
-
|
332
|
-
end
|
333
|
-
|
334
|
-
context 'for a loaded nested constant' do
|
335
|
-
it_behaves_like "loaded constant stubbing", "TestClass::Nested"
|
336
|
-
end
|
337
|
-
|
338
|
-
context 'for a loaded constant prefixed with ::' do
|
339
|
-
it_behaves_like 'loaded constant stubbing', "::TestClass"
|
340
|
-
end
|
341
|
-
|
342
|
-
context 'for an unloaded constant prefixed with ::' do
|
343
|
-
it_behaves_like 'unloaded constant stubbing', "::SomeUndefinedConst"
|
344
|
-
end
|
345
|
-
|
346
|
-
context "for an unloaded constant nested in a module that redefines `send`" do
|
347
|
-
it_behaves_like 'unloaded constant stubbing', "TestClassThatDefinesSend::SomeUndefinedConst"
|
348
|
-
end
|
349
|
-
|
350
|
-
context 'for an unloaded constant with nested name that matches a top-level constant' do
|
351
|
-
it_behaves_like "unloaded constant stubbing", "TestClass::Hash"
|
352
|
-
end
|
353
|
-
|
354
|
-
context 'for a loaded deeply nested constant' do
|
355
|
-
it_behaves_like "loaded constant stubbing", "TestClass::Nested::NestedEvenMore"
|
356
|
-
end
|
357
|
-
|
358
|
-
context 'for an unloaded unnested constant' do
|
359
|
-
it_behaves_like "unloaded constant stubbing", "X"
|
360
|
-
end
|
361
|
-
|
362
|
-
context 'for an unloaded nested constant' do
|
363
|
-
it_behaves_like "unloaded constant stubbing", "X::Y"
|
364
|
-
|
365
|
-
it 'removes the root constant when rspec clears its mocks' do
|
366
|
-
expect(defined?(X)).to be_falsey
|
367
|
-
stub_const("X::Y", 7)
|
368
|
-
reset_rspec_mocks
|
369
|
-
expect(defined?(X)).to be_falsey
|
370
|
-
end
|
371
|
-
end
|
372
|
-
|
373
|
-
context 'for an unloaded deeply nested constant' do
|
374
|
-
it_behaves_like "unloaded constant stubbing", "X::Y::Z"
|
375
|
-
|
376
|
-
it 'removes the root constant when rspec clears its mocks' do
|
377
|
-
expect(defined?(X)).to be_falsey
|
378
|
-
stub_const("X::Y::Z", 7)
|
379
|
-
reset_rspec_mocks
|
380
|
-
expect(defined?(X)).to be_falsey
|
381
|
-
end
|
382
|
-
end
|
383
|
-
|
384
|
-
context 'for an unloaded constant nested within a loaded constant' do
|
385
|
-
it_behaves_like "unloaded constant stubbing", "TestClass::X"
|
386
|
-
|
387
|
-
it 'removes the unloaded constant but leaves the loaded constant when rspec resets its mocks' do
|
388
|
-
expect(defined?(TestClass)).to be_truthy
|
389
|
-
expect(defined?(TestClass::X)).to be_falsey
|
390
|
-
stub_const("TestClass::X", 7)
|
391
|
-
reset_rspec_mocks
|
392
|
-
expect(defined?(TestClass)).to be_truthy
|
393
|
-
expect(defined?(TestClass::X)).to be_falsey
|
394
|
-
end
|
395
|
-
|
396
|
-
it 'raises a helpful error if it cannot be stubbed due to an intermediary constant that is not a module' do
|
397
|
-
expect(TestClass::M).to be_a(Symbol)
|
398
|
-
expect { stub_const("TestClass::M::X", 5) }.to raise_error(/cannot stub/i)
|
399
|
-
end
|
400
|
-
end
|
401
|
-
|
402
|
-
context 'for an unloaded constant nested deeply within a deeply nested loaded constant' do
|
403
|
-
it_behaves_like "unloaded constant stubbing", "TestClass::Nested::NestedEvenMore::X::Y::Z"
|
404
|
-
|
405
|
-
it 'removes the first unloaded constant but leaves the loaded nested constant when rspec resets its mocks' do
|
406
|
-
expect(defined?(TestClass::Nested::NestedEvenMore)).to be_truthy
|
407
|
-
expect(defined?(TestClass::Nested::NestedEvenMore::X)).to be_falsey
|
408
|
-
stub_const("TestClass::Nested::NestedEvenMore::X::Y::Z", 7)
|
409
|
-
reset_rspec_mocks
|
410
|
-
expect(defined?(TestClass::Nested::NestedEvenMore)).to be_truthy
|
411
|
-
expect(defined?(TestClass::Nested::NestedEvenMore::X)).to be_falsey
|
412
|
-
end
|
413
|
-
end
|
414
|
-
end
|
415
|
-
end
|
416
|
-
|
417
|
-
describe Constant do
|
418
|
-
describe ".original" do
|
419
|
-
context 'for a previously defined unstubbed constant' do
|
420
|
-
let(:const) { Constant.original("TestClass::M") }
|
421
|
-
|
422
|
-
it("exposes its name") { expect(const.name).to eq("TestClass::M") }
|
423
|
-
it("indicates it was previously defined") { expect(const).to be_previously_defined }
|
424
|
-
it("indicates it has not been mutated") { expect(const).not_to be_mutated }
|
425
|
-
it("indicates it has not been stubbed") { expect(const).not_to be_stubbed }
|
426
|
-
it("indicates it has not been hidden") { expect(const).not_to be_hidden }
|
427
|
-
it("exposes its original value") { expect(const.original_value).to eq(:m) }
|
428
|
-
end
|
429
|
-
|
430
|
-
context 'for a previously defined stubbed constant' do
|
431
|
-
before { stub_const("TestClass::M", :other) }
|
432
|
-
let(:const) { Constant.original("TestClass::M") }
|
433
|
-
|
434
|
-
it("exposes its name") { expect(const.name).to eq("TestClass::M") }
|
435
|
-
it("indicates it was previously defined") { expect(const).to be_previously_defined }
|
436
|
-
it("indicates it has been mutated") { expect(const).to be_mutated }
|
437
|
-
it("indicates it has been stubbed") { expect(const).to be_stubbed }
|
438
|
-
it("indicates it has not been hidden") { expect(const).not_to be_hidden }
|
439
|
-
it("exposes its original value") { expect(const.original_value).to eq(:m) }
|
440
|
-
end
|
441
|
-
|
442
|
-
context 'for a previously undefined stubbed constant' do
|
443
|
-
before { stub_const("TestClass::Undefined", :other) }
|
444
|
-
let(:const) { Constant.original("TestClass::Undefined") }
|
445
|
-
|
446
|
-
it("exposes its name") { expect(const.name).to eq("TestClass::Undefined") }
|
447
|
-
it("indicates it was not previously defined") { expect(const).not_to be_previously_defined }
|
448
|
-
it("indicates it has been mutated") { expect(const).to be_mutated }
|
449
|
-
it("indicates it has been stubbed") { expect(const).to be_stubbed }
|
450
|
-
it("indicates it has not been hidden") { expect(const).not_to be_hidden }
|
451
|
-
it("returns nil for the original value") { expect(const.original_value).to be_nil }
|
452
|
-
end
|
453
|
-
|
454
|
-
context 'for a previously undefined parent of a stubbed constant' do
|
455
|
-
before { stub_const("TestClass::UndefinedModule::Undefined", :other) }
|
456
|
-
let(:const) { Constant.original("TestClass::UndefinedModule") }
|
457
|
-
|
458
|
-
it("exposes its name") { expect(const.name).to eq("TestClass::UndefinedModule") }
|
459
|
-
it("indicates it was not previously defined") { expect(const).not_to be_previously_defined }
|
460
|
-
it("indicates it has been mutated") { expect(const).to be_mutated }
|
461
|
-
it("indicates it has been stubbed") { expect(const).to be_stubbed }
|
462
|
-
it("indicates it has not been hidden") { expect(const).not_to be_hidden }
|
463
|
-
it("returns nil for the original value") { expect(const.original_value).to be_nil }
|
464
|
-
end
|
465
|
-
|
466
|
-
context 'for a previously undefined unstubbed constant' do
|
467
|
-
let(:const) { Constant.original("TestClass::Undefined") }
|
468
|
-
|
469
|
-
it("exposes its name") { expect(const.name).to eq("TestClass::Undefined") }
|
470
|
-
it("indicates it was not previously defined") { expect(const).not_to be_previously_defined }
|
471
|
-
it("indicates it has not been mutated") { expect(const).not_to be_mutated }
|
472
|
-
it("indicates it has not been stubbed") { expect(const).not_to be_stubbed }
|
473
|
-
it("indicates it has not been hidden") { expect(const).not_to be_hidden }
|
474
|
-
it("returns nil for the original value") { expect(const.original_value).to be_nil }
|
475
|
-
end
|
476
|
-
|
477
|
-
context 'for a previously defined constant that has been stubbed twice' do
|
478
|
-
before { stub_const("TestClass::M", 1) }
|
479
|
-
before { stub_const("TestClass::M", 2) }
|
480
|
-
let(:const) { Constant.original("TestClass::M") }
|
481
|
-
|
482
|
-
it("exposes its name") { expect(const.name).to eq("TestClass::M") }
|
483
|
-
it("indicates it was previously defined") { expect(const).to be_previously_defined }
|
484
|
-
it("indicates it has been mutated") { expect(const).to be_mutated }
|
485
|
-
it("indicates it has been stubbed") { expect(const).to be_stubbed }
|
486
|
-
it("indicates it has not been hidden") { expect(const).not_to be_hidden }
|
487
|
-
it("exposes its original value") { expect(const.original_value).to eq(:m) }
|
488
|
-
end
|
489
|
-
|
490
|
-
context 'for a previously undefined constant that has been stubbed twice' do
|
491
|
-
before { stub_const("TestClass::Undefined", 1) }
|
492
|
-
before { stub_const("TestClass::Undefined", 2) }
|
493
|
-
let(:const) { Constant.original("TestClass::Undefined") }
|
494
|
-
|
495
|
-
it("exposes its name") { expect(const.name).to eq("TestClass::Undefined") }
|
496
|
-
it("indicates it was not previously defined") { expect(const).not_to be_previously_defined }
|
497
|
-
it("indicates it has been mutated") { expect(const).to be_mutated }
|
498
|
-
it("indicates it has been stubbed") { expect(const).to be_stubbed }
|
499
|
-
it("indicates it has not been hidden") { expect(const).not_to be_hidden }
|
500
|
-
it("returns nil for the original value") { expect(const.original_value).to be_nil }
|
501
|
-
end
|
502
|
-
|
503
|
-
context 'for a previously undefined hidden constant' do
|
504
|
-
before { hide_const("SomeUndefinedConst") }
|
505
|
-
let(:const) { Constant.original("SomeUndefinedConst") }
|
506
|
-
|
507
|
-
it("exposes its name") { expect(const.name).to eq("SomeUndefinedConst") }
|
508
|
-
it("indicates it was previously undefined") { expect(const).not_to be_previously_defined }
|
509
|
-
it("indicates it has not been mutated") { expect(const).not_to be_mutated }
|
510
|
-
it("indicates it has not not been stubbed") { expect(const).not_to be_stubbed }
|
511
|
-
it("indicates it has not been hidden") { expect(const).not_to be_hidden }
|
512
|
-
it("returns nil for the original value") { expect(const.original_value).to be_nil }
|
513
|
-
end
|
514
|
-
|
515
|
-
context 'for a previously defined hidden constant' do
|
516
|
-
before { hide_const("TestClass::M") }
|
517
|
-
let(:const) { Constant.original("TestClass::M") }
|
518
|
-
|
519
|
-
it("exposes its name") { expect(const.name).to eq("TestClass::M") }
|
520
|
-
it("indicates it was previously defined") { expect(const).to be_previously_defined }
|
521
|
-
it("indicates it has been mutated") { expect(const).to be_mutated }
|
522
|
-
it("indicates it has not been stubbed") { expect(const).not_to be_stubbed }
|
523
|
-
it("indicates it has been hidden") { expect(const).to be_hidden }
|
524
|
-
it("exposes its original value") { expect(const.original_value).to eq(:m) }
|
525
|
-
end
|
526
|
-
|
527
|
-
context 'for a previously defined constant that has been hidden twice' do
|
528
|
-
before { hide_const("TestClass::M") }
|
529
|
-
before { hide_const("TestClass::M") }
|
530
|
-
let(:const) { Constant.original("TestClass::M") }
|
531
|
-
|
532
|
-
it("exposes its name") { expect(const.name).to eq("TestClass::M") }
|
533
|
-
it("indicates it was previously defined") { expect(const).to be_previously_defined }
|
534
|
-
it("indicates it has been mutated") { expect(const).to be_mutated }
|
535
|
-
it("indicates it has not been stubbed") { expect(const).not_to be_stubbed }
|
536
|
-
it("indicates it has been hidden") { expect(const).to be_hidden }
|
537
|
-
it("exposes its original value") { expect(const.original_value).to eq(:m) }
|
538
|
-
end
|
539
|
-
end
|
540
|
-
end
|
541
|
-
end
|
542
|
-
end
|