Class: Mocha::Mock
- Inherits:
-
Object
- Object
- Mocha::Mock
- Defined in:
- lib/mocha/mock.rb
Overview
Traditional mock object.
All methods return an Expectation which can be further modified by methods on Expectation.
Instance Method Summary (collapse)
-
- (Expectation) expects(method_name_or_hash, backtrace = nil)
(also: #__expects__)
Adds an expectation that the specified method must be called exactly once with any parameters.
-
- (Mock) responds_like(responder)
(also: #quacks_like)
Constrains the Mock instance so that it can only expect or stub methods to which responder responds.
-
- (Mock) responds_like_instance_of(responder_class)
(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.
-
- (Expectation) stubs(method_name_or_hash, backtrace = nil)
(also: #__stubs__)
Adds an expectation that the specified method may be called any number of times with any parameters.
- - (Object) unstub(method_name)
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
- (Object) method_missing(symbol, *arguments, &block)
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
# File 'lib/mocha/mock.rb', line 227 def method_missing(symbol, *arguments, &block) if @responder and not @responder.respond_to?(symbol) raise NoMethodError, "undefined method `#{symbol}' for #{self.mocha_inspect} which responds like #{@responder.mocha_inspect}" end if matching_expectation_allowing_invocation = @expectations.match_allowing_invocation(symbol, *arguments) matching_expectation_allowing_invocation.invoke(&block) else if (matching_expectation = @expectations.match(symbol, *arguments)) || (!matching_expectation && !@everything_stubbed) matching_expectation.invoke(&block) if matching_expectation = UnexpectedInvocation.new(self, symbol, *arguments).to_s << @mockery.mocha_inspect raise ExpectationErrorFactory.build(, caller) end end end |
Instance Method Details
- (Expectation) expects(method_name) - (Expectation) expects(expected_methods_vs_return_values) Also known as: __expects__
Adds an expectation that the specified method must be called exactly once with any parameters.
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/mocha/mock.rb', line 52 def expects(method_name_or_hash, backtrace = nil) iterator = ArgumentIterator.new(method_name_or_hash) iterator.each { |*args| method_name = args.shift ensure_method_not_already_defined(method_name) expectation = Expectation.new(self, method_name, backtrace) expectation.returns(args.shift) if args.length > 0 @expectations.add(expectation) } end |
- (Mock) responds_like(responder) Also known as: quacks_like
Constrains the Mocha::Mock instance so that it can only expect or stub methods to which responder responds. The constraint is only applied at method invocation time.
A NoMethodError will be raised if the responder does not #respond_to? a method invocation (even if the method has been expected or stubbed).
The Mocha::Mock instance will delegate its #respond_to? method to the responder.
161 162 163 164 |
# File 'lib/mocha/mock.rb', line 161 def responds_like(responder) @responder = responder self end |
- (Mock) responds_like_instance_of(responder_class) 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. 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 #respond_to? a method invocation (even if the method has been expected or stubbed).
The Mocha::Mock instance will delegate its #respond_to? method to the responder instance.
192 193 194 |
# File 'lib/mocha/mock.rb', line 192 def responds_like_instance_of(responder_class) responds_like(responder_class.allocate) end |
- (Expectation) stubs(method_name) - (Expectation) stubs(stubbed_methods_vs_return_values) Also known as: __stubs__
Adds an expectation that the specified method may be called any number of times with any parameters.
88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/mocha/mock.rb', line 88 def stubs(method_name_or_hash, backtrace = nil) iterator = ArgumentIterator.new(method_name_or_hash) iterator.each { |*args| method_name = args.shift ensure_method_not_already_defined(method_name) expectation = Expectation.new(self, method_name, backtrace) expectation.at_least(0) expectation.returns(args.shift) if args.length > 0 @expectations.add(expectation) } end |