Module Mocha::ObjectMethods
In: lib/mocha/object_methods.rb
lib/mocha/parameter_matchers/object.rb
lib/mocha/inspect.rb

Methods added to all objects to allow mocking and stubbing on real (i.e. non-mock) objects.

Both {expects} and {stubs} return an {Expectation} which can be further modified by methods on {Expectation}.

Methods

External Aliases

method -> _method
  @private

Public Instance methods

Adds an expectation that the specified method must be called exactly once with any parameters.

The original implementation of the method is replaced during the test and then restored at the end of the test. The temporary replacement method has the same visibility as the original method.

@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 {expects} 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}. @raise [StubbingError] if attempting to stub method which is not allowed.

@example Setting up an expectation on a non-mock object.

  product = Product.new
  product.expects(:save).returns(true)
  assert_equal true, product.save

@example Setting up multiple expectations on a non-mock object.

  product = Product.new
  product.expects(:valid? => true, :save => true)

  # exactly equivalent to

  product = Product.new
  product.expects(:valid?).returns(true)
  product.expects(:save).returns(true)

@see Mock#expects

Adds an expectation that the specified method may be called any number of times with any parameters.

The original implementation of the method is replaced during the test and then restored at the end of the test. The temporary replacement method has the same visibility as the original method.

@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 {stubs} 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}. @raise [StubbingError] if attempting to stub method which is not allowed.

@example Setting up a stubbed methods on a non-mock object.

  product = Product.new
  product.stubs(:save).returns(true)
  assert_equal true, product.save

@example Setting up multiple stubbed methods on a non-mock object.

  product = Product.new
  product.stubs(:valid? => true, :save => true)

  # exactly equivalent to

  product = Product.new
  product.stubs(:valid?).returns(true)
  product.stubs(:save).returns(true)

@see Mock#stubs

Removes the specified stubbed methods (added by calls to {expects} or {stubs}) and all expectations associated with them.

Restores the original behaviour of the methods before they were stubbed. This is normally done automatically at the end of each test, but in some circumstances you may want to do it before the end of the test.

WARNING: If you {unstub} a method which still has unsatisfied expectations, you may be removing the only way those expectations can be satisfied. Use {unstub} with care.

@param [Array<Symbol>] method_names names of methods to unstub.

@example Stubbing and unstubbing a method on a real (non-mock) object.

  multiplier = Multiplier.new
  multiplier.double(2) # => 4
  multiplier.stubs(:double).raises # new behaviour defined
  multiplier.double(2) # => raises exception
  multiplier.unstub(:double) # original behaviour restored
  multiplier.double(2) # => 4

@example Unstubbing multiple methods on a real (non-mock) object.

  multiplier.unstub(:double, :triple)

  # exactly equivalent to

  multiplier.unstub(:double)
  multiplier.unstub(:triple)

[Validate]