Class: Mocha::Mock

### Home Classes Methods

### In Files

- lib/mocha/mock.rb

### Parent

Object

### Methods

- #expects
- #responds_like
- #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::Mock

Traditional mock object.

Methods return an Expectation which can be further modified by methods on Expectation.

### Public Instance Methods

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/String must be called exactly once with any parameters. Returns the new expectation which can be further modified by methods on Expectation.

    object = mock()
    object.expects(:method1)
    object.method1
    # no error raised

    object = mock()
    object.expects(:method1)
    # error raised, because method1 not called exactly once

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`.

    object = mock()
    object.expects(:method1 => :result1, :method2 => :result2)

    # exactly equivalent to

    object = mock()
    object.expects(:method1).returns(:result1)
    object.expects(:method2).returns(:result2)

Aliased by `__expects__`

    # File lib/mocha/mock.rb, line 42
    def expects(method_name_or_hash, backtrace = nil)
      iterator = ArgumentIterator.new(method_name_or_hash)
      iterator.each { |*args|
        method_name = args.shift
        ensure_method_not_already_defined(method_name)
        expectation = Expectation.new(self, method_name, backtrace)
        expectation.returns(args.shift) if args.length > 0
        @expectations.add(expectation)
      }
    end

responds_like(responder) → mock click to toggle source

Constrains the `mock` so that it can only expect or stub methods to which `responder` responds. The constraint is only applied at method invocation time.

A `NoMethodError` will be raised if the `responder` does not `respond_to?` a method invocation (even if the method has been expected or stubbed).

The `mock` will delegate its `respond_to?` method to the `responder`.

    class Sheep
      def chew(grass); end
      def self.number_of_legs; end
    end

    sheep = mock('sheep')
    sheep.expects(:chew)
    sheep.expects(:foo)
    sheep.respond_to?(:chew) # => true
    sheep.respond_to?(:foo) # => true
    sheep.chew
    sheep.foo
    # no error raised

    sheep = mock('sheep')
    sheep.responds_like(Sheep.new)
    sheep.expects(:chew)
    sheep.expects(:foo)
    sheep.respond_to?(:chew) # => true
    sheep.respond_to?(:foo) # => false
    sheep.chew
    sheep.foo # => raises NoMethodError exception

    sheep_class = mock('sheep_class')
    sheep_class.responds_like(Sheep)
    sheep_class.stubs(:number_of_legs).returns(4)
    sheep_class.expects(:foo)
    sheep_class.respond_to?(:number_of_legs) # => true
    sheep_class.respond_to?(:foo) # => false
    assert_equal 4, sheep_class.number_of_legs
    sheep_class.foo # => raises NoMethodError exception

Aliased by `quacks_like`

    # File lib/mocha/mock.rb, line 126
    def responds_like(object)
      @responder = object
      self
    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/String may be called any number of times with any parameters. Returns the new expectation which can be further modified by methods on Expectation.

    object = mock()
    object.stubs(:method1)
    object.method1
    object.method1
    # no error raised

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`.

    object = mock()
    object.stubs(:method1 => :result1, :method2 => :result2)

    # exactly equivalent to

    object = mock()
    object.stubs(:method1).returns(:result1)
    object.stubs(:method2).returns(:result2)

Aliased by `__stubs__`

    # File lib/mocha/mock.rb, line 74
    def stubs(method_name_or_hash, backtrace = nil)
      iterator = ArgumentIterator.new(method_name_or_hash)
      iterator.each { |*args|
        method_name = args.shift
        ensure_method_not_already_defined(method_name)
        expectation = Expectation.new(self, method_name, backtrace)
        expectation.at_least(0)
        expectation.returns(args.shift) if args.length > 0
        @expectations.add(expectation)
      }
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.