lib/mocha/object_methods.rb
# 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.
#
# @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.
#
# @overload def stubs(method_name)
#   @param [Symbol,String] method_name name of stubbed method
# @overload def stubs(stubbed_methods_vs_return_values)
#   @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.
#
# @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
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.
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 .
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 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
Adds an expectation that the specified method may be called any number of times with any parameters.
Adds an expectation that the specified method may be called any number of times with any parameters .
last-built expectation which can be further modified by methods on {Expectation}.
last - built expectation which can be further modified by methods on { Expectation } .
Expectation
[StubbingError] if attempting to stub method which is not allowed.
[ StubbingError ] if attempting to stub method which is not allowed .
stubbed_methods_vs_return_values
overload
def stubs(method_name) @param [Symbol,String] method_name name of stubbed method
def stubs ( method_name ) @param [ Symbol String ] method_name name of stubbed method
overload
def stubs(stubbed_methods_vs_return_values) @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.
def stubs ( stubbed_methods_vs_return_values ) @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 .
example
Setting up a stubbed methods on a non-mock object. product = Product.new product.stubs(:save).returns(true) assert_equal true, product.save
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)
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
None