rspec-mocks 2.11.3 → 3.11.1
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/.document +1 -1
- data/.yardopts +1 -1
- data/Changelog.md +989 -21
- data/{License.txt → LICENSE.md} +5 -3
- data/README.md +260 -73
- data/lib/rspec/mocks/any_instance/chain.rb +58 -24
- data/lib/rspec/mocks/any_instance/error_generator.rb +31 -0
- data/lib/rspec/mocks/any_instance/expect_chain_chain.rb +31 -0
- data/lib/rspec/mocks/any_instance/expectation_chain.rb +23 -30
- data/lib/rspec/mocks/any_instance/message_chains.rb +38 -15
- data/lib/rspec/mocks/any_instance/proxy.rb +116 -0
- data/lib/rspec/mocks/any_instance/recorder.rb +155 -59
- data/lib/rspec/mocks/any_instance/stub_chain.rb +33 -19
- data/lib/rspec/mocks/any_instance/stub_chain_chain.rb +10 -12
- data/lib/rspec/mocks/any_instance.rb +11 -81
- data/lib/rspec/mocks/argument_list_matcher.rb +59 -37
- data/lib/rspec/mocks/argument_matchers.rb +233 -149
- data/lib/rspec/mocks/configuration.rb +212 -0
- data/lib/rspec/mocks/error_generator.rb +304 -49
- data/lib/rspec/mocks/example_methods.rb +361 -22
- data/lib/rspec/mocks/instance_method_stasher.rb +146 -0
- data/lib/rspec/mocks/marshal_extension.rb +41 -0
- data/lib/rspec/mocks/matchers/expectation_customization.rb +20 -0
- data/lib/rspec/mocks/matchers/have_received.rb +134 -0
- data/lib/rspec/mocks/matchers/receive.rb +133 -0
- data/lib/rspec/mocks/matchers/receive_message_chain.rb +82 -0
- data/lib/rspec/mocks/matchers/receive_messages.rb +77 -0
- data/lib/rspec/mocks/message_chain.rb +87 -0
- data/lib/rspec/mocks/message_expectation.rb +648 -314
- data/lib/rspec/mocks/method_double.rb +185 -58
- data/lib/rspec/mocks/method_reference.rb +202 -0
- data/lib/rspec/mocks/minitest_integration.rb +68 -0
- data/lib/rspec/mocks/mutate_const.rb +339 -0
- data/lib/rspec/mocks/object_reference.rb +149 -0
- data/lib/rspec/mocks/order_group.rb +48 -7
- data/lib/rspec/mocks/proxy.rb +405 -74
- data/lib/rspec/mocks/space.rb +219 -15
- data/lib/rspec/mocks/standalone.rb +2 -2
- data/lib/rspec/mocks/syntax.rb +325 -0
- data/lib/rspec/mocks/targets.rb +124 -0
- data/lib/rspec/mocks/test_double.rb +125 -57
- data/lib/rspec/mocks/verifying_double.rb +121 -0
- data/lib/rspec/mocks/verifying_message_expectation.rb +54 -0
- data/lib/rspec/mocks/verifying_proxy.rb +220 -0
- data/lib/rspec/mocks/version.rb +3 -1
- data/lib/rspec/mocks.rb +121 -27
- data.tar.gz.sig +0 -0
- metadata +178 -253
- metadata.gz.sig +3 -0
- data/features/README.md +0 -67
- 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 -60
- data/features/argument_matchers/general_matchers.feature +0 -85
- data/features/argument_matchers/type_matchers.feature +0 -27
- data/features/message_expectations/README.md +0 -69
- data/features/message_expectations/any_instance.feature +0 -21
- data/features/message_expectations/block_local_expectations.feature.pending +0 -55
- data/features/message_expectations/expect_message.feature +0 -94
- 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 -47
- data/features/method_stubs/any_instance.feature +0 -133
- data/features/method_stubs/as_null_object.feature +0 -35
- data/features/method_stubs/simple_return_value.feature +0 -64
- data/features/method_stubs/stub_chain.feature +0 -51
- data/features/method_stubs/stub_implementation.feature +0 -26
- data/features/method_stubs/to_ary.feature +0 -47
- data/features/outside_rspec/configuration.feature +0 -82
- data/features/outside_rspec/standalone.feature +0 -32
- data/features/step_definitions/additional_cli_steps.rb +0 -4
- data/features/stubbing_constants/README.md +0 -62
- data/features/stubbing_constants/stub_defined_constant.feature +0 -79
- data/features/stubbing_constants/stub_undefined_constant.feature +0 -50
- data/features/support/env.rb +0 -6
- data/lib/rspec/mocks/errors.rb +0 -12
- data/lib/rspec/mocks/extensions/instance_exec.rb +0 -34
- data/lib/rspec/mocks/extensions/marshal.rb +0 -23
- data/lib/rspec/mocks/extensions/psych.rb +0 -23
- data/lib/rspec/mocks/framework.rb +0 -21
- data/lib/rspec/mocks/methods.rb +0 -155
- data/lib/rspec/mocks/mock.rb +0 -7
- data/lib/rspec/mocks/serialization.rb +0 -34
- data/lib/rspec/mocks/stashed_instance_method.rb +0 -60
- data/lib/rspec/mocks/stub_const.rb +0 -332
- data/lib/spec/mocks.rb +0 -2
- data/spec/rspec/mocks/and_yield_spec.rb +0 -114
- data/spec/rspec/mocks/any_instance/message_chains_spec.rb +0 -40
- data/spec/rspec/mocks/any_instance_spec.rb +0 -877
- data/spec/rspec/mocks/any_number_of_times_spec.rb +0 -30
- data/spec/rspec/mocks/argument_expectation_spec.rb +0 -34
- data/spec/rspec/mocks/at_least_spec.rb +0 -142
- data/spec/rspec/mocks/at_most_spec.rb +0 -90
- data/spec/rspec/mocks/block_return_value_spec.rb +0 -53
- data/spec/rspec/mocks/bug_report_10260_spec.rb +0 -8
- data/spec/rspec/mocks/bug_report_10263_spec.rb +0 -25
- data/spec/rspec/mocks/bug_report_11545_spec.rb +0 -32
- data/spec/rspec/mocks/bug_report_496_spec.rb +0 -17
- data/spec/rspec/mocks/bug_report_600_spec.rb +0 -22
- data/spec/rspec/mocks/bug_report_7611_spec.rb +0 -16
- data/spec/rspec/mocks/bug_report_8165_spec.rb +0 -31
- data/spec/rspec/mocks/bug_report_830_spec.rb +0 -21
- data/spec/rspec/mocks/bug_report_957_spec.rb +0 -22
- data/spec/rspec/mocks/double_spec.rb +0 -12
- data/spec/rspec/mocks/failing_argument_matchers_spec.rb +0 -95
- data/spec/rspec/mocks/hash_excluding_matcher_spec.rb +0 -67
- data/spec/rspec/mocks/hash_including_matcher_spec.rb +0 -90
- data/spec/rspec/mocks/mock_ordering_spec.rb +0 -103
- data/spec/rspec/mocks/mock_space_spec.rb +0 -58
- data/spec/rspec/mocks/mock_spec.rb +0 -730
- data/spec/rspec/mocks/multiple_return_value_spec.rb +0 -119
- data/spec/rspec/mocks/nil_expectation_warning_spec.rb +0 -62
- data/spec/rspec/mocks/null_object_mock_spec.rb +0 -106
- data/spec/rspec/mocks/once_counts_spec.rb +0 -52
- data/spec/rspec/mocks/options_hash_spec.rb +0 -35
- data/spec/rspec/mocks/partial_mock_spec.rb +0 -171
- data/spec/rspec/mocks/partial_mock_using_mocks_directly_spec.rb +0 -95
- data/spec/rspec/mocks/passing_argument_matchers_spec.rb +0 -142
- 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 -111
- data/spec/rspec/mocks/stash_spec.rb +0 -27
- data/spec/rspec/mocks/stashed_instance_method_spec.rb +0 -53
- data/spec/rspec/mocks/stub_chain_spec.rb +0 -154
- data/spec/rspec/mocks/stub_const_spec.rb +0 -334
- data/spec/rspec/mocks/stub_implementation_spec.rb +0 -81
- data/spec/rspec/mocks/stub_spec.rb +0 -247
- data/spec/rspec/mocks/stubbed_message_expectations_spec.rb +0 -47
- data/spec/rspec/mocks/test_double_spec.rb +0 -57
- data/spec/rspec/mocks/to_ary_spec.rb +0 -40
- data/spec/rspec/mocks/twice_counts_spec.rb +0 -66
- data/spec/rspec/mocks_spec.rb +0 -51
- data/spec/spec_helper.rb +0 -21
@@ -1,209 +0,0 @@
|
|
1
|
-
Feature: receive counts
|
2
|
-
|
3
|
-
Scenario: expect a message once
|
4
|
-
Given a file named "spec/account_spec.rb" with:
|
5
|
-
"""
|
6
|
-
class Account
|
7
|
-
attr_accessor :logger
|
8
|
-
|
9
|
-
def open
|
10
|
-
logger.account_opened
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
14
|
-
describe Account do
|
15
|
-
context "when opened" do
|
16
|
-
it "logger#account_opened was called once" do
|
17
|
-
logger = double("logger")
|
18
|
-
account = Account.new
|
19
|
-
account.logger = logger
|
20
|
-
|
21
|
-
logger.should_receive(:account_opened).once
|
22
|
-
|
23
|
-
account.open
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
27
|
-
"""
|
28
|
-
When I run `rspec spec/account_spec.rb`
|
29
|
-
Then the output should contain "1 example, 0 failures"
|
30
|
-
|
31
|
-
Scenario: expect a message twice
|
32
|
-
Given a file named "spec/account_spec.rb" with:
|
33
|
-
"""
|
34
|
-
class Account
|
35
|
-
attr_accessor :logger
|
36
|
-
|
37
|
-
def open
|
38
|
-
logger.account_opened
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
describe Account do
|
43
|
-
context "when opened" do
|
44
|
-
it "logger#account_opened was called once" do
|
45
|
-
logger = double("logger")
|
46
|
-
account = Account.new
|
47
|
-
account.logger = logger
|
48
|
-
|
49
|
-
logger.should_receive(:account_opened).twice
|
50
|
-
|
51
|
-
account.open
|
52
|
-
account.open
|
53
|
-
end
|
54
|
-
end
|
55
|
-
end
|
56
|
-
"""
|
57
|
-
When I run `rspec spec/account_spec.rb`
|
58
|
-
Then the output should contain "1 example, 0 failures"
|
59
|
-
|
60
|
-
Scenario: expect a message 3 times
|
61
|
-
Given a file named "spec/account_spec.rb" with:
|
62
|
-
"""
|
63
|
-
class Account
|
64
|
-
attr_accessor :logger
|
65
|
-
|
66
|
-
def open
|
67
|
-
logger.account_opened
|
68
|
-
end
|
69
|
-
end
|
70
|
-
|
71
|
-
describe Account do
|
72
|
-
context "when opened" do
|
73
|
-
it "logger#account_opened was called once" do
|
74
|
-
logger = double("logger")
|
75
|
-
account = Account.new
|
76
|
-
account.logger = logger
|
77
|
-
|
78
|
-
logger.should_receive(:account_opened).exactly(3).times
|
79
|
-
|
80
|
-
account.open
|
81
|
-
account.open
|
82
|
-
account.open
|
83
|
-
end
|
84
|
-
end
|
85
|
-
end
|
86
|
-
"""
|
87
|
-
When I run `rspec spec/account_spec.rb`
|
88
|
-
Then the output should contain "1 example, 0 failures"
|
89
|
-
|
90
|
-
Scenario: expect a message at least (:once)
|
91
|
-
Given a file named "spec/account_spec.rb" with:
|
92
|
-
"""
|
93
|
-
class Account
|
94
|
-
attr_accessor :logger
|
95
|
-
|
96
|
-
def open
|
97
|
-
logger.account_opened
|
98
|
-
end
|
99
|
-
end
|
100
|
-
|
101
|
-
describe Account do
|
102
|
-
context "when opened" do
|
103
|
-
it "logger#account_opened was called once" do
|
104
|
-
logger = double("logger")
|
105
|
-
account = Account.new
|
106
|
-
account.logger = logger
|
107
|
-
|
108
|
-
logger.should_receive(:account_opened).at_least(:once)
|
109
|
-
|
110
|
-
account.open
|
111
|
-
end
|
112
|
-
end
|
113
|
-
end
|
114
|
-
"""
|
115
|
-
When I run `rspec spec/account_spec.rb`
|
116
|
-
Then the output should contain "1 example, 0 failures"
|
117
|
-
|
118
|
-
|
119
|
-
Scenario: expect a message at least (n) times
|
120
|
-
Given a file named "spec/account_spec.rb" with:
|
121
|
-
"""
|
122
|
-
class Account
|
123
|
-
attr_accessor :logger
|
124
|
-
|
125
|
-
def open
|
126
|
-
logger.account_opened
|
127
|
-
end
|
128
|
-
end
|
129
|
-
|
130
|
-
describe Account do
|
131
|
-
context "when opened" do
|
132
|
-
it "logger#account_opened was called once" do
|
133
|
-
logger = double("logger")
|
134
|
-
account = Account.new
|
135
|
-
account.logger = logger
|
136
|
-
|
137
|
-
logger.should_receive(:account_opened).at_least(3).times
|
138
|
-
|
139
|
-
# Note that I am calling method under test 4 times
|
140
|
-
# and I specified it to be called at least 3 times
|
141
|
-
account.open
|
142
|
-
account.open
|
143
|
-
account.open
|
144
|
-
account.open
|
145
|
-
end
|
146
|
-
end
|
147
|
-
end
|
148
|
-
"""
|
149
|
-
When I run `rspec spec/account_spec.rb`
|
150
|
-
Then the output should contain "1 example, 0 failures"
|
151
|
-
|
152
|
-
Scenario: expect a message at most (:once)
|
153
|
-
Given a file named "spec/account_spec.rb" with:
|
154
|
-
"""
|
155
|
-
class Account
|
156
|
-
attr_accessor :logger
|
157
|
-
|
158
|
-
def open
|
159
|
-
logger.account_opened
|
160
|
-
end
|
161
|
-
end
|
162
|
-
|
163
|
-
describe Account do
|
164
|
-
context "when opened" do
|
165
|
-
it "logger#account_opened was called once" do
|
166
|
-
logger = double("logger")
|
167
|
-
account = Account.new
|
168
|
-
account.logger = logger
|
169
|
-
|
170
|
-
logger.should_receive(:account_opened).at_most(:once)
|
171
|
-
|
172
|
-
account.open
|
173
|
-
account.open
|
174
|
-
end
|
175
|
-
end
|
176
|
-
end
|
177
|
-
"""
|
178
|
-
When I run `rspec spec/account_spec.rb`
|
179
|
-
Then the output should contain "expected: 1 time"
|
180
|
-
And the output should contain "received: 2 times"
|
181
|
-
|
182
|
-
Scenario: expect a message at most (n) times
|
183
|
-
Given a file named "spec/account_spec.rb" with:
|
184
|
-
"""
|
185
|
-
class Account
|
186
|
-
attr_accessor :logger
|
187
|
-
|
188
|
-
def open
|
189
|
-
logger.account_opened
|
190
|
-
end
|
191
|
-
end
|
192
|
-
|
193
|
-
describe Account do
|
194
|
-
context "when opened" do
|
195
|
-
it "logger#account_opened was called once" do
|
196
|
-
logger = double("logger")
|
197
|
-
account = Account.new
|
198
|
-
account.logger = logger
|
199
|
-
|
200
|
-
logger.should_receive(:account_opened).at_most(2).times
|
201
|
-
|
202
|
-
account.open
|
203
|
-
account.open
|
204
|
-
end
|
205
|
-
end
|
206
|
-
end
|
207
|
-
"""
|
208
|
-
When I run `rspec spec/account_spec.rb`
|
209
|
-
Then the output should contain "1 example, 0 failures"
|
@@ -1,50 +0,0 @@
|
|
1
|
-
Feature: warn when expectation is set on nil
|
2
|
-
|
3
|
-
Scenario: nil instance variable
|
4
|
-
Given a file named "example_spec.rb" with:
|
5
|
-
"""
|
6
|
-
RSpec.configure {|c| c.mock_with :rspec}
|
7
|
-
describe "something" do
|
8
|
-
it "does something" do
|
9
|
-
@i_do_not_exist.should_receive(:foo)
|
10
|
-
@i_do_not_exist.foo
|
11
|
-
end
|
12
|
-
end
|
13
|
-
"""
|
14
|
-
When I run `rspec example_spec.rb`
|
15
|
-
Then the output should contain "An expectation of :foo was set on nil"
|
16
|
-
|
17
|
-
Scenario: allow
|
18
|
-
Given a file named "example_spec.rb" with:
|
19
|
-
"""
|
20
|
-
RSpec.configure {|c| c.mock_with :rspec}
|
21
|
-
describe "something" do
|
22
|
-
it "does something" do
|
23
|
-
allow_message_expectations_on_nil
|
24
|
-
nil.should_receive(:foo)
|
25
|
-
nil.foo
|
26
|
-
end
|
27
|
-
end
|
28
|
-
"""
|
29
|
-
When I run `rspec example_spec.rb`
|
30
|
-
Then the output should not contain "An expectation"
|
31
|
-
|
32
|
-
Scenario: allow in one example, but not on another
|
33
|
-
Given a file named "example_spec.rb" with:
|
34
|
-
"""
|
35
|
-
RSpec.configure {|c| c.mock_with :rspec}
|
36
|
-
describe "something" do
|
37
|
-
it "does something (foo)" do
|
38
|
-
allow_message_expectations_on_nil
|
39
|
-
nil.should_receive(:foo)
|
40
|
-
nil.foo
|
41
|
-
end
|
42
|
-
it "does something (bar)" do
|
43
|
-
nil.should_receive(:bar)
|
44
|
-
nil.bar
|
45
|
-
end
|
46
|
-
end
|
47
|
-
"""
|
48
|
-
When I run `rspec example_spec.rb`
|
49
|
-
Then the output should contain "An expectation of :bar"
|
50
|
-
And the output should not contain "An expectation of :foo"
|
@@ -1,47 +0,0 @@
|
|
1
|
-
### Stub return values
|
2
|
-
|
3
|
-
# create a double
|
4
|
-
obj = double()
|
5
|
-
|
6
|
-
# specify a return value
|
7
|
-
obj.stub(:message) { :value }
|
8
|
-
obj.stub(:message => :value)
|
9
|
-
obj.stub(:message).and_return(:value)
|
10
|
-
|
11
|
-
These forms are somewhat interchangeable. The difference is that the
|
12
|
-
block contents are evaluated lazily when the `obj` receives the
|
13
|
-
`message` message, whereas the others are evaluated as they are read.
|
14
|
-
|
15
|
-
### Fake implementation
|
16
|
-
|
17
|
-
obj.stub(:message) do |arg1, arg2|
|
18
|
-
# set expectations about the args in this block
|
19
|
-
# and/or set a return value
|
20
|
-
end
|
21
|
-
|
22
|
-
### Raising/Throwing
|
23
|
-
|
24
|
-
obj.stub(:message).and_raise("this error")
|
25
|
-
obj.stub(:message).and_throw(:this_symbol)
|
26
|
-
|
27
|
-
You can also use the block format:
|
28
|
-
|
29
|
-
obj.stub(:message) { raise "this error" }
|
30
|
-
obj.stub(:message) { throw :this_symbol }
|
31
|
-
|
32
|
-
### Argument constraints
|
33
|
-
|
34
|
-
#### Explicit arguments
|
35
|
-
|
36
|
-
obj.stub(:message).with('an argument') { ... }
|
37
|
-
obj.stub(:message).with('more_than', 'one_argument') { ... }
|
38
|
-
|
39
|
-
#### Argument matchers
|
40
|
-
|
41
|
-
obj.stub(:message).with(anything()) { ... }
|
42
|
-
obj.stub(:message).with(an_instance_of(Money)) { ... }
|
43
|
-
obj.stub(:message).with(hash_including(:a => 'b')) { ... }
|
44
|
-
|
45
|
-
#### Regular expressions
|
46
|
-
|
47
|
-
obj.stub(:message).with(/abc/) { ... }
|
@@ -1,133 +0,0 @@
|
|
1
|
-
Feature: stub on any instance of a class
|
2
|
-
|
3
|
-
Use `any_instance.stub` on a class to tell any instance of that class to
|
4
|
-
return a value (or values) in response to a given message. If no instance
|
5
|
-
receives the message, nothing happens.
|
6
|
-
|
7
|
-
Messages can be stubbed on any class, including those in Ruby's core library.
|
8
|
-
|
9
|
-
Scenario: any_instance stub with a single return value
|
10
|
-
Given a file named "example_spec.rb" with:
|
11
|
-
"""
|
12
|
-
describe "any_instance.stub" do
|
13
|
-
it "returns the specified value on any instance of the class" do
|
14
|
-
Object.any_instance.stub(:foo).and_return(:return_value)
|
15
|
-
|
16
|
-
o = Object.new
|
17
|
-
o.foo.should eq(:return_value)
|
18
|
-
end
|
19
|
-
end
|
20
|
-
"""
|
21
|
-
When I run `rspec example_spec.rb`
|
22
|
-
Then the examples should all pass
|
23
|
-
|
24
|
-
Scenario: any_instance stub with a hash
|
25
|
-
Given a file named "example_spec.rb" with:
|
26
|
-
"""
|
27
|
-
describe "any_instance.stub" do
|
28
|
-
context "with a hash" do
|
29
|
-
it "returns the hash values on any instance of the class" do
|
30
|
-
Object.any_instance.stub(:foo => 'foo', :bar => 'bar')
|
31
|
-
|
32
|
-
o = Object.new
|
33
|
-
o.foo.should eq('foo')
|
34
|
-
o.bar.should eq('bar')
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
38
|
-
"""
|
39
|
-
When I run `rspec example_spec.rb`
|
40
|
-
Then the examples should all pass
|
41
|
-
|
42
|
-
Scenario: any_instance stub with specific arguments matchers
|
43
|
-
Given a file named "example_spec.rb" with:
|
44
|
-
"""
|
45
|
-
describe "any_instance.stub" do
|
46
|
-
context "with arguments" do
|
47
|
-
it "returns the stubbed value when arguments match" do
|
48
|
-
Object.any_instance.stub(:foo).with(:param_one, :param_two).and_return(:result_one)
|
49
|
-
Object.any_instance.stub(:foo).with(:param_three, :param_four).and_return(:result_two)
|
50
|
-
|
51
|
-
o = Object.new
|
52
|
-
o.foo(:param_one, :param_two).should eq(:result_one)
|
53
|
-
o.foo(:param_three, :param_four).should eq(:result_two)
|
54
|
-
end
|
55
|
-
end
|
56
|
-
end
|
57
|
-
"""
|
58
|
-
When I run `rspec example_spec.rb`
|
59
|
-
Then the examples should all pass
|
60
|
-
|
61
|
-
Scenario: any_instance unstub
|
62
|
-
Given a file named "example_spec.rb" with:
|
63
|
-
"""
|
64
|
-
describe "any_instance.unstub" do
|
65
|
-
it "unstubs a stubbed method" do
|
66
|
-
class Object
|
67
|
-
def foo
|
68
|
-
:foo
|
69
|
-
end
|
70
|
-
end
|
71
|
-
|
72
|
-
Object.any_instance.stub(:foo)
|
73
|
-
Object.any_instance.unstub(:foo)
|
74
|
-
|
75
|
-
Object.new.foo.should eq(:foo)
|
76
|
-
end
|
77
|
-
end
|
78
|
-
"""
|
79
|
-
When I run `rspec example_spec.rb`
|
80
|
-
Then the examples should all pass
|
81
|
-
|
82
|
-
Scenario: any_instance unstub
|
83
|
-
Given a file named "example_spec.rb" with:
|
84
|
-
"""
|
85
|
-
describe "any_instance.unstub" do
|
86
|
-
context "with both an expectation and a stub already set" do
|
87
|
-
it "only removes the stub" do
|
88
|
-
class Object
|
89
|
-
def foo
|
90
|
-
:foo
|
91
|
-
end
|
92
|
-
end
|
93
|
-
Object.any_instance.should_receive(:foo).and_return(:bar)
|
94
|
-
Object.any_instance.stub(:foo)
|
95
|
-
Object.any_instance.unstub(:foo)
|
96
|
-
|
97
|
-
Object.new.foo.should eq(:bar)
|
98
|
-
end
|
99
|
-
end
|
100
|
-
end
|
101
|
-
"""
|
102
|
-
When I run `rspec example_spec.rb`
|
103
|
-
Then the examples should all pass
|
104
|
-
|
105
|
-
Scenario: stub a chain of methods an any instance
|
106
|
-
Given a file named "stub_chain_spec.rb" with:
|
107
|
-
"""
|
108
|
-
describe "stubbing a chain of methods" do
|
109
|
-
context "given symbols representing methods" do
|
110
|
-
it "returns the correct value" do
|
111
|
-
Object.any_instance.stub_chain(:one, :two, :three).and_return(:four)
|
112
|
-
Object.new.one.two.three.should eq(:four)
|
113
|
-
end
|
114
|
-
end
|
115
|
-
|
116
|
-
context "given a hash at the end" do
|
117
|
-
it "returns the correct value" do
|
118
|
-
Object.any_instance.stub_chain(:one, :two, :three => :four)
|
119
|
-
Object.new.one.two.three.should eq(:four)
|
120
|
-
end
|
121
|
-
end
|
122
|
-
|
123
|
-
context "given a string of methods separated by dots" do
|
124
|
-
it "returns the correct value" do
|
125
|
-
Object.any_instance.stub_chain("one.two.three").and_return(:four)
|
126
|
-
Object.new.one.two.three.should eq(:four)
|
127
|
-
end
|
128
|
-
end
|
129
|
-
end
|
130
|
-
"""
|
131
|
-
When I run `rspec stub_chain_spec.rb`
|
132
|
-
Then the examples should all pass
|
133
|
-
|
@@ -1,35 +0,0 @@
|
|
1
|
-
Feature: as_null_object
|
2
|
-
|
3
|
-
Use the `as_null_object` method to ignore any messages that aren't explicitly
|
4
|
-
set as stubs or message expectations.
|
5
|
-
|
6
|
-
EXCEPTION: `to_ary` will raise a NoMethodError unless explicitly stubbed in
|
7
|
-
order to support `flatten` on an Array containing a double.
|
8
|
-
|
9
|
-
Scenario: double acting as_null_object
|
10
|
-
Given a file named "as_null_object_spec.rb" with:
|
11
|
-
"""
|
12
|
-
describe "a double with as_null_object called" do
|
13
|
-
let(:null_object) { double('null object').as_null_object }
|
14
|
-
|
15
|
-
it "responds to any method that is not defined" do
|
16
|
-
null_object.should respond_to(:an_undefined_method)
|
17
|
-
end
|
18
|
-
|
19
|
-
it "allows explicit stubs" do
|
20
|
-
null_object.stub(:foo) { "bar" }
|
21
|
-
null_object.foo.should eq("bar")
|
22
|
-
end
|
23
|
-
|
24
|
-
it "allows explicit expectations" do
|
25
|
-
null_object.should_receive(:something)
|
26
|
-
null_object.something
|
27
|
-
end
|
28
|
-
|
29
|
-
it "supports Array#flatten" do
|
30
|
-
[null_object].flatten.should eq([null_object])
|
31
|
-
end
|
32
|
-
end
|
33
|
-
"""
|
34
|
-
When I run `rspec as_null_object_spec.rb`
|
35
|
-
Then the examples should all pass
|
@@ -1,64 +0,0 @@
|
|
1
|
-
Feature: stub with a simple return value
|
2
|
-
|
3
|
-
Use the `stub` method on a test double or a real object to tell the object to
|
4
|
-
return a value (or values) in response to a given message. Nothing happens if
|
5
|
-
the message is never received.
|
6
|
-
|
7
|
-
Scenario: stub with no return value
|
8
|
-
Given a file named "example_spec.rb" with:
|
9
|
-
"""
|
10
|
-
describe "a stub with no return value specified" do
|
11
|
-
let(:collaborator) { double("collaborator") }
|
12
|
-
|
13
|
-
it "returns nil" do
|
14
|
-
collaborator.stub(:message)
|
15
|
-
collaborator.message.should be(nil)
|
16
|
-
end
|
17
|
-
end
|
18
|
-
"""
|
19
|
-
When I run `rspec example_spec.rb`
|
20
|
-
Then the examples should all pass
|
21
|
-
|
22
|
-
Scenario: stubs with return values
|
23
|
-
Given a file named "example_spec.rb" with:
|
24
|
-
"""
|
25
|
-
describe "a stub with a return value" do
|
26
|
-
context "specified in a block" do
|
27
|
-
it "returns the specified value" do
|
28
|
-
collaborator = double("collaborator")
|
29
|
-
collaborator.stub(:message) { :value }
|
30
|
-
collaborator.message.should eq(:value)
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
context "specified with #and_return" do
|
35
|
-
it "returns the specified value" do
|
36
|
-
collaborator = double("collaborator")
|
37
|
-
collaborator.stub(:message).and_return(:value)
|
38
|
-
collaborator.message.should eq(:value)
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
context "specified with a hash passed to #stub" do
|
43
|
-
it "returns the specified value" do
|
44
|
-
collaborator = double("collaborator")
|
45
|
-
collaborator.stub(:message_1 => :value_1, :message_2 => :value_2)
|
46
|
-
collaborator.message_1.should eq(:value_1)
|
47
|
-
collaborator.message_2.should eq(:value_2)
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
context "specified with a hash passed to #double" do
|
52
|
-
it "returns the specified value" do
|
53
|
-
collaborator = double("collaborator",
|
54
|
-
:message_1 => :value_1,
|
55
|
-
:message_2 => :value_2
|
56
|
-
)
|
57
|
-
collaborator.message_1.should eq(:value_1)
|
58
|
-
collaborator.message_2.should eq(:value_2)
|
59
|
-
end
|
60
|
-
end
|
61
|
-
end
|
62
|
-
"""
|
63
|
-
When I run `rspec example_spec.rb`
|
64
|
-
Then the examples should all pass
|
@@ -1,51 +0,0 @@
|
|
1
|
-
Feature: stub a chain of methods
|
2
|
-
|
3
|
-
Use the `stub_chain` method to stub a chain of two or more methods in one
|
4
|
-
statement. Method chains are considered a design smell, but it's not really
|
5
|
-
the method chain itself that is the problem - it's the dependency chain
|
6
|
-
represented by a chain of messages to different objects:
|
7
|
-
|
8
|
-
foo.get_bar.get_baz
|
9
|
-
|
10
|
-
This is a Law of Demeter violation if `get_bar` returns an object other than
|
11
|
-
`foo`, and `get_baz` returns yet another object.
|
12
|
-
|
13
|
-
Fluent interfaces look similar from a caller's perspective, but don't
|
14
|
-
represent a dependency chain (the caller depends only on the object it is
|
15
|
-
calling). Consider this common example from Ruby on Rails:
|
16
|
-
|
17
|
-
Article.recent.by(current_user)
|
18
|
-
|
19
|
-
The `recent` and `by` methods return the same object, so this is not a Law of
|
20
|
-
Demeter violation.
|
21
|
-
|
22
|
-
Scenario: stub a chain of methods
|
23
|
-
Given a file named "stub_chain_spec.rb" with:
|
24
|
-
"""
|
25
|
-
describe "stubbing a chain of methods" do
|
26
|
-
subject { Object.new }
|
27
|
-
|
28
|
-
context "given symbols representing methods" do
|
29
|
-
it "returns the correct value" do
|
30
|
-
subject.stub_chain(:one, :two, :three).and_return(:four)
|
31
|
-
subject.one.two.three.should eq(:four)
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
context "given a hash at the end" do
|
36
|
-
it "returns the correct value" do
|
37
|
-
subject.stub_chain(:one, :two, :three => :four)
|
38
|
-
subject.one.two.three.should eq(:four)
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
context "given a string of methods separated by dots" do
|
43
|
-
it "returns the correct value" do
|
44
|
-
subject.stub_chain("one.two.three").and_return(:four)
|
45
|
-
subject.one.two.three.should eq(:four)
|
46
|
-
end
|
47
|
-
end
|
48
|
-
end
|
49
|
-
"""
|
50
|
-
When I run `rspec stub_chain_spec.rb`
|
51
|
-
Then the examples should all pass
|
@@ -1,26 +0,0 @@
|
|
1
|
-
Feature: stub with substitute implementation
|
2
|
-
|
3
|
-
You can stub an implementation of a method (a.k.a. fake) by passing a block
|
4
|
-
to the `stub` method.
|
5
|
-
|
6
|
-
Scenario: stub implementation
|
7
|
-
Given a file named "stub_implementation_spec.rb" with:
|
8
|
-
"""
|
9
|
-
describe "a stubbed implementation" do
|
10
|
-
it "works" do
|
11
|
-
object = Object.new
|
12
|
-
object.stub(:foo) do |arg|
|
13
|
-
if arg == :this
|
14
|
-
"got this"
|
15
|
-
elsif arg == :that
|
16
|
-
"got that"
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
object.foo(:this).should eq("got this")
|
21
|
-
object.foo(:that).should eq("got that")
|
22
|
-
end
|
23
|
-
end
|
24
|
-
"""
|
25
|
-
When I run `rspec stub_implementation_spec.rb`
|
26
|
-
Then the output should contain "1 example, 0 failures"
|
@@ -1,47 +0,0 @@
|
|
1
|
-
Feature: double handling to_ary
|
2
|
-
|
3
|
-
Ruby implicitly sends `to_ary` to all of the objects in an `Array` when the
|
4
|
-
array receives `flatten`:
|
5
|
-
|
6
|
-
[obj].flatten # => obj.to_ary
|
7
|
-
|
8
|
-
If `to_ary` raises a `NoMethodError`, Ruby sees that the object can not be coerced
|
9
|
-
into an array and moves on to the next object.
|
10
|
-
|
11
|
-
To support this, an RSpec double will raise a NoMethodError when it receives
|
12
|
-
`to_ary` _even if it is set `as_null_object`_, unless `to_ary` is explicitly
|
13
|
-
stubbed.
|
14
|
-
|
15
|
-
Scenario: double receiving to_ary
|
16
|
-
Given a file named "example.rb" with:
|
17
|
-
"""
|
18
|
-
describe "#to_ary" do
|
19
|
-
shared_examples "to_ary" do
|
20
|
-
it "raises a NoMethodError" do
|
21
|
-
expect { obj.to_ary }.to raise_error(NoMethodError)
|
22
|
-
end
|
23
|
-
|
24
|
-
it "can be overridden with a stub" do
|
25
|
-
obj.stub(:to_ary) { :non_nil_value }
|
26
|
-
obj.to_ary.should be(:non_nil_value)
|
27
|
-
end
|
28
|
-
|
29
|
-
it "supports Array#flatten" do
|
30
|
-
obj = double('foo')
|
31
|
-
[obj].flatten.should eq([obj])
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
context "sent to a double as_null_object" do
|
36
|
-
let(:obj) { double('obj').as_null_object }
|
37
|
-
include_examples "to_ary"
|
38
|
-
end
|
39
|
-
|
40
|
-
context "sent to a double without as_null_object" do
|
41
|
-
let(:obj) { double('obj') }
|
42
|
-
include_examples "to_ary"
|
43
|
-
end
|
44
|
-
end
|
45
|
-
"""
|
46
|
-
When I run `rspec example.rb`
|
47
|
-
Then the examples should all pass
|