Traditional mock object.
All methods return an {Expectation} which can be further modified by methods on {Expectation}.
@private
# File lib/mocha/mock.rb, line 165 165: def initialize(mockery, name = nil, &block) 166: @mockery = mockery 167: @name = name || DefaultName.new(self) 168: @expectations = ExpectationList.new 169: @everything_stubbed = false 170: @responder = nil 171: instance_eval(&block) if block 172: end
@private
# File lib/mocha/mock.rb, line 184 184: def __expectations__ 185: @expectations 186: end
@private
# File lib/mocha/mock.rb, line 224 224: def __verified__?(assertion_counter = nil) 225: @expectations.verified?(assertion_counter) 226: end
@private
# File lib/mocha/mock.rb, line 244 244: def any_expectations? 245: @expectations.any? 246: end
@private
# File lib/mocha/mock.rb, line 239 239: def ensure_method_not_already_defined(method_name) 240: self.__metaclass__.send(:undef_method, method_name) if self.__metaclass__.method_defined?(method_name) 241: end
Adds an expectation that the specified method must be called exactly once with any parameters.
@param [Symbol,String] method_name name of expected method @param [Hash] expected_methods_vs_return_values expected method name symbols as keys and corresponding return values as values - these expectations are setup as if {#} were called multiple times.
@overload def expects(method_name) @overload def expects(expected_methods_vs_return_values) @return [Expectation] last-built expectation which can be further modified by methods on {Expectation}.
@example Expected method invoked once so no error raised
object = mock() object.expects(:expected_method) object.expected_method
@example Expected method not invoked so error raised
object = mock() object.expects(:expected_method) # error raised when test completes, because expected_method not called exactly once
@example Expected method invoked twice so error raised
object = mock() object.expects(:expected_method) object.expected_method object.expected_method # => error raised when expected method invoked second time
@example Setup multiple expectations using expected_methods_vs_return_values.
object = mock() object.expects(:expected_method_one => :result_one, :expected_method_two => :result_two) # is exactly equivalent to object = mock() object.expects(:expected_method_one).returns(:result_one) object.expects(:expected_method_two).returns(:result_two)
# File lib/mocha/mock.rb, line 51 51: def expects(method_name_or_hash, backtrace = nil) 52: iterator = ArgumentIterator.new(method_name_or_hash) 53: iterator.each { |*args| 54: method_name = args.shift 55: ensure_method_not_already_defined(method_name) 56: expectation = Expectation.new(self, method_name, backtrace) 57: expectation.returns(args.shift) if args.length > 0 58: @expectations.add(expectation) 59: } 60: end
@private
# File lib/mocha/mock.rb, line 234 234: def inspect 235: mocha_inspect 236: end
@private
# File lib/mocha/mock.rb, line 194 194: def method_missing(symbol, *arguments, &block) 195: if @responder and not @responder.respond_to?(symbol) 196: raise NoMethodError, "undefined method `#{symbol}' for #{self.mocha_inspect} which responds like #{@responder.mocha_inspect}" 197: end 198: if matching_expectation_allowing_invocation = @expectations.match_allowing_invocation(symbol, *arguments) 199: matching_expectation_allowing_invocation.invoke(&block) 200: else 201: if (matching_expectation = @expectations.match(symbol, *arguments)) || (!matching_expectation && !@everything_stubbed) 202: matching_expectation.invoke(&block) if matching_expectation 203: message = UnexpectedInvocation.new(self, symbol, *arguments).to_s 204: message << @mockery.mocha_inspect 205: raise ExpectationError.new(message, caller) 206: end 207: end 208: end
@private
# File lib/mocha/mock.rb, line 229 229: def mocha_inspect 230: @name.mocha_inspect 231: end
@private
# File lib/mocha/mock.rb, line 211 211: def respond_to?(symbol, include_private = false) 212: if @responder then 213: if @responder.method(:respond_to?).arity > 1 214: @responder.respond_to?(symbol, include_private) 215: else 216: @responder.respond_to?(symbol) 217: end 218: else 219: @everything_stubbed || @expectations.matches_method?(symbol) 220: end 221: end
Constrains the {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 # a method invocation (even if the method has been expected or stubbed).
The {Mock} instance will delegate its # method to the responder.
@param [Object, #] responder an object used to determine whether {Mock} instance should # to an invocation. @return [Mock] the same {Mock} instance, thereby allowing invocations of other {Mock} methods to be chained.
@example Normal mocking
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
@example Using {#} with an instance method
class Sheep def chew(grass); end end 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
@example Using {#} with a class method
class Sheep def self.number_of_legs; end end 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
# File lib/mocha/mock.rb, line 159 159: def responds_like(responder) 160: @responder = responder 161: self 162: end
@private
# File lib/mocha/mock.rb, line 189 189: def stub_everything 190: @everything_stubbed = true 191: end
Adds an expectation that the specified method may be called any number of times with any parameters.
@param [Symbol,String] method_name name of stubbed method @param [Hash] stubbed_methods_vs_return_values stubbed method name symbols as keys and corresponding return values as values - these stubbed methods are setup as if {#} were called multiple times.
@overload def stubs(method_name) @overload def stubs(stubbed_methods_vs_return_values) @return [Expectation] last-built expectation which can be further modified by methods on {Expectation}.
@example No error raised however many times stubbed method is invoked
object = mock() object.stubs(:stubbed_method) object.stubbed_method object.stubbed_method # no error raised
@example Setup multiple expectations using stubbed_methods_vs_return_values.
object = mock() object.stubs(:stubbed_method_one => :result_one, :stubbed_method_two => :result_two) # is exactly equivalent to object = mock() object.stubs(:stubbed_method_one).returns(:result_one) object.stubs(:stubbed_method_two).returns(:result_two)
# File lib/mocha/mock.rb, line 87 87: def stubs(method_name_or_hash, backtrace = nil) 88: iterator = ArgumentIterator.new(method_name_or_hash) 89: iterator.each { |*args| 90: method_name = args.shift 91: ensure_method_not_already_defined(method_name) 92: expectation = Expectation.new(self, method_name, backtrace) 93: expectation.at_least(0) 94: expectation.returns(args.shift) if args.length > 0 95: @expectations.add(expectation) 96: } 97: end
Removes the specified stubbed method (added by calls to {#} or {#}) and all expectations associated with it.
@param [Symbol] method_name name of method to unstub.
@example Invoking an unstubbed method causes error to be raised
object = mock('mock') do object.stubs(:stubbed_method).returns(:result_one) object.stubbed_method # => :result_one object.unstub(:stubbed_method) object.stubbed_method # => unexpected invocation: #<Mock:mock>.stubbed_method()
# File lib/mocha/mock.rb, line 109 109: def unstub(method_name) 110: @expectations.remove_all_matching_method(method_name) 111: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.