lib/mocha/object_methods.rb
# 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.
#
# @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 expects(method_name)
#   @param [Symbol,String] method_name name of expected method
# @overload def expects(expected_methods_vs_return_values)
#   @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.
#
# @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 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.
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 .
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 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
Adds an expectation that the specified method must be called exactly once with any parameters.
Adds an expectation that the specified method must be called exactly once 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 .
expected_methods_vs_return_values
overload
def expects(method_name) @param [Symbol,String] method_name name of expected method
def expects ( method_name ) @param [ Symbol String ] method_name name of expected method
overload
def expects(expected_methods_vs_return_values) @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.
def expects ( expected_methods_vs_return_values ) @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 .
example
Setting up an expectation on a non-mock object. product = Product.new product.expects(:save).returns(true) assert_equal true, product.save
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)
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
None