module Mocha::ObjectMethods

def expects(expected_methods_vs_return_values)

Other tags:
    See: Mock#expects -

Other tags:
    Example: Setting up multiple expectations on a non-mock object. -
    Example: Setting up an expectation on a non-mock object. -

Parameters:
  • expected_methods_vs_return_values (Hash) -- expected method name symbols as keys and corresponding return values as values - these expectations are setup as if {#expects} were called multiple times.
  • method_name (Symbol, String) -- name of expected method

Overloads:
  • def expects(expected_methods_vs_return_values)
  • def expects(method_name)

Raises:
  • (StubbingError) - if attempting to stub method which is not allowed.

Returns:
  • (Expectation) - last-built expectation which can be further modified by methods on {Expectation}.
def expects(expected_methods_vs_return_values)
  if expected_methods_vs_return_values.to_s =~ /the[^a-z]*spanish[^a-z]*inquisition/i
    raise ExpectationErrorFactory.build('NOBODY EXPECTS THE SPANISH INQUISITION!')
  end
  if frozen?
    raise StubbingError.new("can't stub method on frozen object: #{mocha_inspect}", caller)
  end
  expectation = nil
  mockery = Mocha::Mockery.instance
  iterator = ArgumentIterator.new(expected_methods_vs_return_values)
  iterator.each do |*args|
    method_name = args.shift
    mockery.on_stubbing(self, method_name)
    method = stubba_method.new(stubba_object, method_name)
    mockery.stubba.stub(method)
    expectation = mocha.expects(method_name, caller)
    expectation.returns(args.shift) unless args.empty?
  end
  expectation
end

def mocha(instantiate = true)

Other tags:
    Private: -
def mocha(instantiate = true)
  if instantiate
    @mocha ||= Mocha::Mockery.instance.mock_impersonating(self)
  else
    defined?(@mocha) ? @mocha : nil
  end
end

def reset_mocha

Other tags:
    Private: -
def reset_mocha
  @mocha = nil
end

def stubba_class

Other tags:
    Private: -
def stubba_class
  singleton_class
end

def stubba_method

Other tags:
    Private: -
def stubba_method
  Mocha::InstanceMethod
end

def stubba_object

Other tags:
    Private: -
def stubba_object
  self
end

def stubs(stubbed_methods_vs_return_values)

Other tags:
    See: Mock#stubs -

Other tags:
    Example: Setting up multiple stubbed methods on a non-mock object. -
    Example: Setting up a stubbed methods on a non-mock object. -

Parameters:
  • stubbed_methods_vs_return_values (Hash) -- stubbed method name symbols as keys and corresponding return values as values - these stubbed methods are setup as if {#stubs} were called multiple times.
  • method_name (Symbol, String) -- name of stubbed method

Overloads:
  • def stubs(stubbed_methods_vs_return_values)
  • def stubs(method_name)

Raises:
  • (StubbingError) - if attempting to stub method which is not allowed.

Returns:
  • (Expectation) - last-built expectation which can be further modified by methods on {Expectation}.
def stubs(stubbed_methods_vs_return_values)
  if frozen?
    raise StubbingError.new("can't stub method on frozen object: #{mocha_inspect}", caller)
  end
  expectation = nil
  mockery = Mocha::Mockery.instance
  iterator = ArgumentIterator.new(stubbed_methods_vs_return_values)
  iterator.each do |*args|
    method_name = args.shift
    mockery.on_stubbing(self, method_name)
    method = stubba_method.new(stubba_object, method_name)
    mockery.stubba.stub(method)
    expectation = mocha.stubs(method_name, caller)
    expectation.returns(args.shift) unless args.empty?
  end
  expectation
end

def unstub(*method_names)

Other tags:
    Example: Unstubbing multiple methods on a real (non-mock) object. -
    Example: Stubbing and unstubbing a method on a real (non-mock) object. -

Parameters:
  • method_names (Array) -- names of methods to unstub.
def unstub(*method_names)
  mockery = Mocha::Mockery.instance
  method_names.each do |method_name|
    method = stubba_method.new(stubba_object, method_name)
    mockery.stubba.unstub(method)
  end
end