Class: Mocha::Mock
- Inherits:
-
Object
- Object
- Mocha::Mock
- Defined in:
- lib/mocha/mock.rb
Overview
Traditional mock object.
#expects and #stubs return an Expectation which can be further modified by methods on Expectation.
#responds_like and #responds_like_instance_of both return a Mock, and can therefore, be chained to the original creation methods in API. They force the mock to indicate what it is supposed to be mocking, thus making it a safer verifying mock. They check that the underlying responder
will actually respond to the methods being stubbed, throwing a NoMethodError
upon invocation otherwise.
Stubs and expectations are basically the same thing. A stub is just an expectation of zero or more invocations. The #stubs method is syntactic sugar to make the intent of the test more explicit.
When a method is invoked on a mock object, the mock object searches through its expectations from newest to oldest to find one that matches the invocation. After the invocation, the matching expectation might stop matching further invocations. For example, an expects(:foo).once expectation only matches once and will be ignored on future invocations while an expects(:foo).at_least_once expectation will always be matched against invocations.
However, note that if the expectation that matches the invocation has a cardinality of “never”, then an unexpected invocation error is reported.
This scheme allows you to:
-
Set up default stubs in your the
setup
method of your test class and override some of those stubs in individual tests. -
Set up different
once
expectations for the same method with different action per invocation. However, it’s better to use the Expectation#returns method with multiple arguments to do this, as described below.
However, there are some possible “gotchas” caused by this scheme:
-
if you create an expectation and then a stub for the same method, the stub will always override the expectation and the expectation will never be met.
-
if you create a stub and then an expectation for the same method, the expectation will match, and when it stops matching the stub will be used instead, possibly masking test failures.
-
if you create different expectations for the same method, they will be invoked in the opposite order than that in which they were specified, rather than the same order.
The best thing to do is not set up multiple expectations and stubs for the same method with exactly the same matchers. Instead, use the Expectation#returns method with multiple arguments to create multiple actions for a method. You can also chain multiple calls to Expectation#returns and Expectation#raises (along with syntactic sugar Expectation#then if desired).
If you want to specify more complex ordering or order invocations across different mock objects, use the Expectation#in_sequence method to explicitly define a total or partial ordering of invocations.
Instance Method Summary collapse
-
#expects(method_name_or_hash, backtrace = nil) ⇒ Expectation
(also: #__expects__)
Adds an expectation that the specified method must be called exactly once with any parameters.
-
#responds_like(responder) ⇒ Mock
(also: #quacks_like)
Constrains the Mock instance so that it can only expect or stub methods to which
responder
responds publicly. -
#responds_like_instance_of(responder_class) ⇒ Mock
(also: #quacks_like_instance_of)
Constrains the Mock instance so that it can only expect or stub methods to which an instance of the
responder_class
responds publicly. -
#stubs(method_name_or_hash, backtrace = nil) ⇒ Expectation
(also: #__stubs__)
Adds an expectation that the specified method may be called any number of times with any parameters.
- #unstub(*method_names) ⇒ Object
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(symbol, *arguments, &block) ⇒ Object
318 319 320 |
# File 'lib/mocha/mock.rb', line 318 def method_missing(symbol, *arguments, &block) # rubocop:disable Style/MethodMissingSuper handle_method_call(symbol, arguments, block) end |
Instance Method Details
#expects(method_name) ⇒ Expectation #expects(expected_methods_vs_return_values) ⇒ Expectation Also known as: __expects__
Adds an expectation that the specified method must be called exactly once with any parameters.
114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/mocha/mock.rb', line 114 def expects(method_name_or_hash, backtrace = nil) expectation = nil iterator = ArgumentIterator.new(method_name_or_hash) iterator.each do |*args| method_name = args.shift ensure_method_not_already_defined(method_name) expectation = Expectation.new(self, method_name, backtrace) expectation.in_sequence(@mockery.sequences.last) if @mockery.sequences.any? expectation.returns(args.shift) unless args.empty? @expectations.add(expectation) end expectation end |
#responds_like(responder) ⇒ Mock Also known as: quacks_like
Constrains the Mocha::Mock instance so that it can only expect or stub methods to which responder
responds publicly. The constraint is only applied at method invocation time.
A NoMethodError
will be raised if the responder
does not publicly #respond_to?
the invoked method (even if the method has been expected or stubbed).
The Mocha::Mock instance will delegate its #respond_to?
method to the responder
. However, the include_all
parameter is not passed through, so only public methods on the responder
will be considered.
Note that the methods on responder
are never actually invoked.
241 242 243 244 |
# File 'lib/mocha/mock.rb', line 241 def responds_like(responder) @responder = responder self end |
#responds_like_instance_of(responder_class) ⇒ Mock Also known as: quacks_like_instance_of
Constrains the Mocha::Mock instance so that it can only expect or stub methods to which an instance of the responder_class
responds publicly. The constraint is only applied at method invocation time. Note that the responder instance is instantiated using Class#allocate.
A NoMethodError
will be raised if the responder instance does not publicly #respond_to?
the invoked method (even if the method has been expected or stubbed).
The Mocha::Mock instance will delegate its #respond_to?
method to the responder instance. However, the include_all
parameter is not passed through, so only public methods on the responder
will be considered.
Note that the methods on the responder instance are never actually invoked.
274 275 276 |
# File 'lib/mocha/mock.rb', line 274 def responds_like_instance_of(responder_class) responds_like(responder_class.allocate) end |
#stubs(method_name) ⇒ Expectation #stubs(stubbed_methods_vs_return_values) ⇒ Expectation Also known as: __stubs__
Adds an expectation that the specified method may be called any number of times with any parameters.
153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/mocha/mock.rb', line 153 def stubs(method_name_or_hash, backtrace = nil) expectation = nil iterator = ArgumentIterator.new(method_name_or_hash) iterator.each do |*args| method_name = args.shift ensure_method_not_already_defined(method_name) expectation = Expectation.new(self, method_name, backtrace) expectation.at_least(0) expectation.in_sequence(@mockery.sequences.last) if @mockery.sequences.any? expectation.returns(args.shift) unless args.empty? @expectations.add(expectation) end expectation end |
#unstub(*method_names) ⇒ Object
186 187 188 189 190 |
# File 'lib/mocha/mock.rb', line 186 def unstub(*method_names) method_names.each do |method_name| @expectations.remove_all_matching_method(method_name) end end |