Module: Mocha::ObjectMethods

### Home Classes Methods

### In Files

- lib/mocha/inspect.rb
- lib/mocha/metaclass.rb
- lib/mocha/parameter_matchers/object.rb
- lib/mocha/object.rb

### Methods

- #__metaclass__
- #expects
- #mocha_inspect
- #stubs

### Files

- COPYING
- README

### Class/Module Index

Quicksearch

- Mocha
- Mocha::API
- Mocha::AnyInstanceMethod
- Mocha::ArgumentIterator
- Mocha::ArrayMethods
- Mocha::BacktraceFilter
- Mocha::Cardinality
- Mocha::Central
- Mocha::ChangeStateSideEffect
- Mocha::ClassMethod
- Mocha::ClassMethods
- Mocha::Configuration
- Mocha::DateMethods
- Mocha::DefaultName
- Mocha::Deprecation
- Mocha::Expectation
- Mocha::ExpectationError
- Mocha::ExpectationList
- Mocha::HashMethods
- Mocha::ImpersonatingAnyInstanceName
- Mocha::ImpersonatingName
- Mocha::InStateOrderingConstraint
- Mocha::InstanceMethod
- Mocha::Integration
- Mocha::Integration::MiniTest
- Mocha::Integration::MiniTest::AssertionCounter
- Mocha::Integration::MiniTest::Version131AndAbove
- Mocha::Integration::TestUnit
- Mocha::Integration::TestUnit::AssertionCounter
- Mocha::Integration::TestUnit::GemVersion200
- Mocha::Integration::TestUnit::GemVersion201AndAbove
- Mocha::Integration::TestUnit::RubyVersion185AndBelow
- Mocha::Integration::TestUnit::RubyVersion186AndAbove
- Mocha::Logger
- Mocha::MethodMatcher
- Mocha::Mock
- Mocha::Mockery
- Mocha::ModuleMethod
- Mocha::Name
- Mocha::ObjectMethods
- Mocha::ParameterMatchers
- Mocha::ParametersMatcher
- Mocha::PrettyParameters
- Mocha::Sequence
- Mocha::Sequence::InSequenceOrderingConstraint
- Mocha::StateMachine
- Mocha::StringMethods
- Mocha::StubbingError
- Mocha::TimeMethods
- Mocha::UnexpectedInvocation
- MiniTest
- MiniTest::Unit
- MiniTest::Unit::TestCase
- Test
- Test::Unit
- Test::Unit::TestCase
- Array
- Date
- Hash
- Object
- String
- Time

No matching classes.

# Mocha::ObjectMethods

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.

### Public Instance Methods

__metaclass__() click to toggle source

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

expects(method_name) → expectation click to toggle source
expects(method_names_vs_return_values) → last expectation

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)

    # File lib/mocha/object.rb, line 53
    def expects(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.expects(method_name, caller)
        expectation.returns(args.shift) if args.length > 0
      }
      expectation
    end

mocha_inspect() click to toggle source

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

stubs(method_name) → expectation click to toggle source
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)

    # 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

[Validate]

Generated with the Darkfish Rdoc Generator 2.