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

Methods added all objects to allow mocking and stubbing on real objects.

Methods return a Mocha::Expectation which can be further modified by methods on Mocha::Expectation.

Methods

Public Instance methods

[Source]

   # File lib/mocha/metaclass.rb, line 4
4:     def __metaclass__
5:       class << self; self; end
6:     end

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

  product = Product.new
  product.expects(: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.expects(:valid? => true, :save => true)

  # exactly equivalent to

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

[Source]

    # File lib/mocha/object.rb, line 53
53:     def expects(method_name_or_hash)
54:       expectation = nil
55:       mockery = Mocha::Mockery.instance
56:       iterator = ArgumentIterator.new(method_name_or_hash)
57:       iterator.each { |*args|
58:         method_name = args.shift
59:         mockery.on_stubbing(self, method_name)
60:         method = stubba_method.new(stubba_object, method_name)
61:         mockery.stubba.stub(method)
62:         expectation = mocha.expects(method_name, caller)
63:         expectation.returns(args.shift) if args.length > 0
64:       }
65:       expectation
66:     end

[Source]

    # File lib/mocha/inspect.rb, line 6
 6:     def mocha_inspect
 7:       address = self.__id__ * 2
 8:       address += 0x100000000 if address < 0
 9:       inspect =~ /#</ ? "#<#{self.class}:0x#{'%x' % address}>" : inspect
10:     end

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)

[Source]

     # File lib/mocha/object.rb, line 90
 90:     def stubs(method_name_or_hash)
 91:       expectation = nil
 92:       mockery = Mocha::Mockery.instance
 93:       iterator = ArgumentIterator.new(method_name_or_hash)
 94:       iterator.each { |*args|
 95:         method_name = args.shift
 96:         mockery.on_stubbing(self, method_name)
 97:         method = stubba_method.new(stubba_object, method_name)
 98:         mockery.stubba.stub(method)
 99:         expectation = mocha.stubs(method_name, caller)
100:         expectation.returns(args.shift) if args.length > 0
101:       }
102:       expectation
103:     end

[Validate]