Class: Mocha::Mock
Overview
Traditional mock object.
Methods return an Expectation which can be further modified by methods on Expectation.
Instance Attribute Summary collapse
-
#everything_stubbed ⇒ Object
readonly
Returns the value of attribute everything_stubbed.
Instance Method Summary collapse
- #__expectations__ ⇒ Object
- #__verified__?(assertion_counter = nil) ⇒ Boolean
- #any_expectations? ⇒ Boolean
- #ensure_method_not_already_defined(method_name) ⇒ Object
-
#expects(method_name_or_hash, backtrace = nil) ⇒ Object
(also: #__expects__)
:call-seq: expects(method_name) -> expectation expects(method_names_vs_return_values) -> last expectation.
-
#initialize(mockery, name = nil, &block) ⇒ Mock
constructor
:stopdoc:.
- #inspect ⇒ Object
- #method_missing(symbol, *arguments, &block) ⇒ Object
- #mocha_inspect ⇒ Object
- #respond_to?(symbol, include_private = false) ⇒ Boolean
-
#responds_like(object) ⇒ Object
(also: #quacks_like)
:call-seq: responds_like(responder) -> mock.
- #stub_everything ⇒ Object
-
#stubs(method_name_or_hash, backtrace = nil) ⇒ Object
(also: #__stubs__)
:call-seq: stubs(method_name) -> expectation stubs(method_names_vs_return_values) -> last expectation.
- #unstub(method_name) ⇒ Object
Constructor Details
#initialize(mockery, name = nil, &block) ⇒ Mock
:stopdoc:
136 137 138 139 140 141 142 143 |
# File 'lib/mocha/mock.rb', line 136 def initialize(mockery, name = nil, &block) @mockery = mockery @name = name || DefaultName.new(self) @expectations = ExpectationList.new @everything_stubbed = false @responder = nil instance_eval(&block) if block end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(symbol, *arguments, &block) ⇒ Object
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/mocha/mock.rb', line 161 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 ExpectationError.new(, caller) end end end |
Instance Attribute Details
#everything_stubbed ⇒ Object (readonly)
Returns the value of attribute everything_stubbed.
145 146 147 |
# File 'lib/mocha/mock.rb', line 145 def everything_stubbed @everything_stubbed end |
Instance Method Details
#__expectations__ ⇒ Object
153 154 155 |
# File 'lib/mocha/mock.rb', line 153 def __expectations__ @expectations end |
#__verified__?(assertion_counter = nil) ⇒ Boolean
189 190 191 |
# File 'lib/mocha/mock.rb', line 189 def __verified__?(assertion_counter = nil) @expectations.verified?(assertion_counter) end |
#any_expectations? ⇒ Boolean
205 206 207 |
# File 'lib/mocha/mock.rb', line 205 def any_expectations? @expectations.any? end |
#ensure_method_not_already_defined(method_name) ⇒ Object
201 202 203 |
# File 'lib/mocha/mock.rb', line 201 def ensure_method_not_already_defined(method_name) self..send(:undef_method, method_name) if self..method_defined?(method_name) end |
#expects(method_name_or_hash, backtrace = nil) ⇒ Object Also known as: __expects__
:call-seq: expects(method_name) -> expectation
expects(method_names_vs_return_values) -> last expectation
Adds an expectation that a method identified by method_name
Symbol/String must be called exactly once with any parameters. Returns the new expectation which can be further modified by methods on Expectation.
object = mock()
object.expects(:method1)
object.method1
# no error raised
object = mock()
object.expects(:method1)
# error raised, because method1 not called exactly once
If method_names_vs_return_values
is a Hash
, an expectation will be set up for each entry using the key as method_name
and value as return_value
.
object = mock()
object.expects(:method1 => :result1, :method2 => :result2)
# exactly equivalent to
object = mock()
object.expects(:method1).returns(:result1)
object.expects(:method2).returns(:result2)
Aliased by _\expects\_
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/mocha/mock.rb', line 41 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 |
#inspect ⇒ Object
197 198 199 |
# File 'lib/mocha/mock.rb', line 197 def inspect mocha_inspect end |
#mocha_inspect ⇒ Object
193 194 195 |
# File 'lib/mocha/mock.rb', line 193 def mocha_inspect @name.mocha_inspect end |
#respond_to?(symbol, include_private = false) ⇒ Boolean
177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/mocha/mock.rb', line 177 def respond_to?(symbol, include_private = false) if @responder then if @responder.method(:respond_to?).arity > 1 @responder.respond_to?(symbol, include_private) else @responder.respond_to?(symbol) end else @everything_stubbed || @expectations.matches_method?(symbol) end end |
#responds_like(object) ⇒ Object Also known as: quacks_like
:call-seq: responds_like(responder) -> mock
Constrains the mock
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 mock
will delegate its respond_to?
method to the responder
.
class Sheep
def chew(grass); end
def self.number_of_legs; end
end
sheep = mock('sheep')
sheep.expects(:chew)
sheep.expects(:foo)
sheep.respond_to?(:chew) # => true
sheep.respond_to?(:foo) # => true
sheep.chew
sheep.foo
# no error raised
sheep = mock('sheep')
sheep.responds_like(Sheep.new)
sheep.expects(:chew)
sheep.expects(:foo)
sheep.respond_to?(:chew) # => true
sheep.respond_to?(:foo) # => false
sheep.chew
sheep.foo # => raises NoMethodError exception
sheep_class = mock('sheep_class')
sheep_class.responds_like(Sheep)
sheep_class.stubs(:number_of_legs).returns(4)
sheep_class.expects(:foo)
sheep_class.respond_to?(:number_of_legs) # => true
sheep_class.respond_to?(:foo) # => false
assert_equal 4, sheep_class.number_of_legs
sheep_class.foo # => raises NoMethodError exception
Aliased by quacks_like
129 130 131 132 |
# File 'lib/mocha/mock.rb', line 129 def responds_like(object) @responder = object self end |
#stub_everything ⇒ Object
157 158 159 |
# File 'lib/mocha/mock.rb', line 157 def stub_everything @everything_stubbed = true end |
#stubs(method_name_or_hash, backtrace = nil) ⇒ Object Also known as: __stubs__
:call-seq: stubs(method_name) -> expectation
stubs(method_names_vs_return_values) -> last expectation
Adds an expectation that a method identified by method_name
Symbol/String may be called any number of times with any parameters. Returns the new expectation which can be further modified by methods on Expectation.
object = mock()
object.stubs(:method1)
object.method1
object.method1
# no error raised
If method_names_vs_return_values
is a Hash
, an expectation will be set up for each entry using the key as method_name
and value as return_value
.
object = mock()
object.stubs(:method1 => :result1, :method2 => :result2)
# exactly equivalent to
object = mock()
object.stubs(:method1).returns(:result1)
object.stubs(:method2).returns(:result2)
Aliased by _\stubs\_
73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/mocha/mock.rb', line 73 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 |
#unstub(method_name) ⇒ Object
85 86 87 |
# File 'lib/mocha/mock.rb', line 85 def unstub(method_name) @expectations.remove_all_matching_method(method_name) end |