Method: Mocha::ObjectMethods#stubs

Defined in:
lib/mocha/object.rb

#stubs(method_name_or_hash) ⇒ Object

:call-seq: stubs(method_name) -> expectation

stubs(method_names_vs_return_values) -> last expectation

Adds an expectation that a method identified by method_name Symbol may be called any number of times with any parameters. Returns the new expectation which can be further modified by methods on Mocha::Expectation.

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

The original implementation of Product#save is replaced temporarily.

The original implementation of Product#save is restored at the end of the test.

If method_names_vs_return_values is a Hash, an expectation will be set up for each entry using the key as method_name and value as return_value.

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)


90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/mocha/object.rb', line 90

def stubs(method_name_or_hash)
  expectation = nil
  mockery = Mocha::Mockery.instance
  iterator = ArgumentIterator.new(method_name_or_hash)
  iterator.each { |*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) if args.length > 0
  }
  expectation
end