Module: Mocha::API

### Home Classes Methods

### In Files

- lib/mocha/api.rb

### Methods

- ::included
- #mock
- #sequence
- #states
- #stub
- #stub_everything

### Included Modules

- Mocha::ParameterMatchers
- Mocha::Hooks

### Class/Module Index

Quicksearch

- Mocha
- Mocha::API
- Mocha::AnyInstanceMethod
- Mocha::AnyInstanceReceiver
- Mocha::ArgumentIterator
- Mocha::ArrayMethods
- Mocha::BacktraceFilter
- Mocha::Cardinality
- Mocha::Central
- Mocha::ChangeStateSideEffect
- Mocha::ClassMethod
- Mocha::ClassMethods
- Mocha::ClassMethods::AnyInstance
- Mocha::Configuration
- Mocha::DateMethods
- Mocha::Debug
- Mocha::DefaultName
- Mocha::DefaultReceiver
- Mocha::Deprecation
- Mocha::Detection
- Mocha::Detection::MiniTest
- Mocha::Detection::TestUnit
- Mocha::ExceptionRaiser
- Mocha::Expectation
- Mocha::ExpectationError
- Mocha::ExpectationErrorFactory
- Mocha::ExpectationList
- Mocha::HashMethods
- Mocha::Hooks
- Mocha::ImpersonatingAnyInstanceName
- Mocha::ImpersonatingName
- Mocha::InStateOrderingConstraint
- Mocha::InstanceMethod
- Mocha::Integration
- Mocha::Integration::AssertionCounter
- Mocha::Integration::MiniTest
- Mocha::Integration::MiniTest::Adapter
- Mocha::Integration::MiniTest::Nothing
- Mocha::Integration::MiniTest::Version13
- Mocha::Integration::MiniTest::Version13::RunMethodPatch
- Mocha::Integration::MiniTest::Version140
- Mocha::Integration::MiniTest::Version140::RunMethodPatch
- Mocha::Integration::MiniTest::Version141
- Mocha::Integration::MiniTest::Version141::RunMethodPatch
- Mocha::Integration::MiniTest::Version142To172
- Mocha::Integration::MiniTest::Version142To172::RunMethodPatch
- Mocha::Integration::MiniTest::Version200
- Mocha::Integration::MiniTest::Version200::RunMethodPatch
- Mocha::Integration::MiniTest::Version201To222
- Mocha::Integration::MiniTest::Version201To222::RunMethodPatch
- Mocha::Integration::MiniTest::Version2110To2111
- Mocha::Integration::MiniTest::Version2110To2111::RunMethodPatch
- Mocha::Integration::MiniTest::Version2112To320
- Mocha::Integration::MiniTest::Version2112To320::RunMethodPatch
- Mocha::Integration::MiniTest::Version230To2101
- Mocha::Integration::MiniTest::Version230To2101::RunMethodPatch
- Mocha::Integration::MonkeyPatcher
- Mocha::Integration::TestUnit
- Mocha::Integration::TestUnit::Adapter
- Mocha::Integration::TestUnit::GemVersion200
- Mocha::Integration::TestUnit::GemVersion200::RunMethodPatch
- Mocha::Integration::TestUnit::GemVersion201To202
- Mocha::Integration::TestUnit::GemVersion201To202::RunMethodPatch
- Mocha::Integration::TestUnit::GemVersion203To220
- Mocha::Integration::TestUnit::GemVersion203To220::RunMethodPatch
- Mocha::Integration::TestUnit::GemVersion230To250
- Mocha::Integration::TestUnit::GemVersion230To250::RunMethodPatch
- Mocha::Integration::TestUnit::Nothing
- Mocha::Integration::TestUnit::RubyVersion185AndBelow
- Mocha::Integration::TestUnit::RubyVersion185AndBelow::RunMethodPatch
- Mocha::Integration::TestUnit::RubyVersion186AndAbove
- Mocha::Integration::TestUnit::RubyVersion186AndAbove::RunMethodPatch
- Mocha::Logger
- Mocha::MethodMatcher
- Mocha::Mock
- Mocha::Mockery
- Mocha::ModuleMethod
- Mocha::ModuleMethods
- Mocha::MultipleYields
- Mocha::Name
- Mocha::NoYields
- Mocha::ObjectMethods
- Mocha::ObjectReceiver
- Mocha::ParameterMatchers
- Mocha::ParameterMatchers::AllOf
- Mocha::ParameterMatchers::AnyOf
- Mocha::ParameterMatchers::AnyParameters
- Mocha::ParameterMatchers::Anything
- Mocha::ParameterMatchers::Base
- Mocha::ParameterMatchers::Equals
- Mocha::ParameterMatchers::HasEntries
- Mocha::ParameterMatchers::HasEntry
- Mocha::ParameterMatchers::HasKey
- Mocha::ParameterMatchers::HasValue
- Mocha::ParameterMatchers::Includes
- Mocha::ParameterMatchers::InstanceOf
- Mocha::ParameterMatchers::IsA
- Mocha::ParameterMatchers::KindOf
- Mocha::ParameterMatchers::Not
- Mocha::ParameterMatchers::Optionally
- Mocha::ParameterMatchers::QueryStringMatches
- Mocha::ParameterMatchers::RegexpMatches
- Mocha::ParameterMatchers::RespondsWith
- Mocha::ParameterMatchers::YamlEquivalent
- Mocha::ParametersMatcher
- Mocha::PrettyParameters
- Mocha::ReturnValues
- Mocha::Sequence
- Mocha::Sequence::InSequenceOrderingConstraint
- Mocha::SingleReturnValue
- Mocha::SingleYield
- Mocha::StateMachine
- Mocha::StateMachine::State
- Mocha::StateMachine::StatePredicate
- Mocha::StringMethods
- Mocha::StubbingError
- Mocha::Thrower
- Mocha::TimeMethods
- Mocha::UnexpectedInvocation
- Mocha::YieldParameters
- Array
- Date
- Hash
- Object
- String
- Time

No matching classes.

# Mocha::API

Methods added to +Test::Unit::TestCase+, +MiniTest::Unit::TestCase+ or equivalent.

### Public Class Methods

included(mod) click to toggle source

@private

    # File lib/mocha/api.rb, line 18
    def self.included(mod)
      Object.send(:include, Mocha::ObjectMethods)
      Module.send(:include, Mocha::ModuleMethods)
      Class.send(:include, Mocha::ClassMethods)
    end

### Public Instance Methods

mock(*arguments, &block) click to toggle source

Builds a new mock object

@param [String] name identifies mock object in error messages. @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 {Mock#expects} were called multiple times. @yield optional block to be evaluated against the mock object instance, giving an alternative way to setup expectations. @return [Mock] a new mock object

@overload def mock(name, &block) @overload def mock(expected_methods_vs_return_values = {}, &block) @overload def mock(name, expected_methods_vs_return_values = {}, &block)

@example Using expected_methods_vs_return_values Hash to setup expectations.

    def test_motor_starts_and_stops
      motor = mock('motor', :start => true, :stop => true)
      assert motor.start
      assert motor.stop
      # an error will be raised unless both Motor#start and Motor#stop have been called
    end

@example Using the optional block to setup expectations & stubbed methods.

    def test_motor_starts_and_stops
      motor = mock('motor') do
        expects(:start).with(100.rpm).returns(true)
        stubs(:stop).returns(true)
      end
      assert motor.start(100.rpm)
      assert motor.stop
      # an error will only be raised if Motor#start(100.rpm) has not been called
    end

    # File lib/mocha/api.rb, line 52
    def mock(*arguments, &block)
      name = arguments.shift if arguments.first.is_a?(String)
      expectations = arguments.shift || {}
      mock = name ? Mockery.instance.named_mock(name, &block) : Mockery.instance.unnamed_mock(&block)
      mock.expects(expectations)
      mock
    end

sequence(name) click to toggle source

Builds a new sequence which can be used to constrain the order in which expectations can occur.

Specify that an expected invocation must occur within a named {Sequence} by using {Expectation#in_sequence}.

@return [Sequence] a new sequence

@see Expectation#in_sequence

@example Ensure methods on egg are invoked in correct order.

    breakfast = sequence('breakfast')

    egg = mock('egg') do
      expects(:crack).in_sequence(breakfast)
      expects(:fry).in_sequence(breakfast)
      expects(:eat).in_sequence(breakfast)
    end

    # File lib/mocha/api.rb, line 140
    def sequence(name)
      Sequence.new(name)
    end

states(name) click to toggle source

Builds a new state machine which can be used to constrain the order in which expectations can occur.

Specify the initial state of the state machine by using {StateMachine#starts_as}.

Specify that an expected invocation should change the state of the state machine by using {Expectation#then}.

Specify that an expected invocation should be constrained to occur within a particular `state` by using {Expectation#when}.

A test can contain multiple state machines.

@return [StateMachine] a new state machine

@see Expectation#then @see Expectation#when @see StateMachine @example Constrain expected invocations to occur in particular states.

    power = states('power').starts_as('off')

    radio = mock('radio') do
      expects(:switch_on).then(power.is('on'))
      expects(:select_channel).with('BBC Radio 4').when(power.is('on'))
      expects(:adjust_volume).with(+5).when(power.is('on'))
      expects(:select_channel).with('BBC World Service').when(power.is('on'))
      expects(:adjust_volume).with(-5).when(power.is('on'))
      expects(:switch_off).then(power.is('off'))
    end

    # File lib/mocha/api.rb, line 170
    def states(name)
      Mockery.instance.new_state_machine(name)
    end

stub(*arguments, &block) click to toggle source

Builds a new mock object

@param [String] name identifies mock object in error messages. @param [Hash] stubbed_methods_vs_return_values stubbed method name symbols as keys and corresponding return values as values - these stubbed methods are setup as if {Mock#stubs} were called multiple times. @yield optional block to be evaluated against the mock object instance, giving an alternative way to setup stubbed methods. @return [Mock] a new mock object

@overload def stub(name, &block) @overload def stub(stubbed_methods_vs_return_values = {}, &block) @overload def stub(name, stubbed_methods_vs_return_values = {}, &block)

@example Using stubbed_methods_vs_return_values Hash to setup stubbed methods.

    def test_motor_starts_and_stops
      motor = mock('motor', :start => true, :stop => true)
      assert motor.start
      assert motor.stop
      # an error will not be raised even if either Motor#start or Motor#stop has not been called
    end

@example Using the optional block to setup expectations & stubbed methods.

    def test_motor_starts_and_stops
      motor = mock('motor') do
        expects(:start).with(100.rpm).returns(true)
        stubs(:stop).returns(true)
      end
      assert motor.start(100.rpm)
      assert motor.stop
      # an error will only be raised if Motor#start(100.rpm) has not been called
    end

    # File lib/mocha/api.rb, line 89
    def stub(*arguments, &block)
      name = arguments.shift if arguments.first.is_a?(String)
      expectations = arguments.shift || {}
      stub = name ? Mockery.instance.named_mock(name, &block) : Mockery.instance.unnamed_mock(&block)
      stub.stubs(expectations)
      stub
    end

stub_everything(*arguments, &block) click to toggle source

Builds a mock object that accepts calls to any method. By default it will return `nil` for any method call.

@param [String] name identifies mock object in error messages. @param [Hash] stubbed_methods_vs_return_values stubbed method name symbols as keys and corresponding return values as values - these stubbed methods are setup as if {Mock#stubs} were called multiple times. @yield optional block to be evaluated against the mock object instance, giving an alternative way to setup stubbed methods. @return [Mock] a new mock object

@overload def stub_everything(name, &block) @overload def stub_everything(stubbed_methods_vs_return_values = {}, &block) @overload def stub_everything(name, stubbed_methods_vs_return_values = {}, &block)

@example Ignore invocations of irrelevant methods.

    def test_motor_stops
      motor = stub_everything('motor', :stop => true)
      assert_nil motor.irrelevant_method_1 # => no error raised
      assert_nil motor.irrelevant_method_2 # => no error raised
      assert motor.stop
    end

    # File lib/mocha/api.rb, line 115
    def stub_everything(*arguments, &block)
      name = arguments.shift if arguments.first.is_a?(String)
      expectations = arguments.shift || {}
      stub = name ? Mockery.instance.named_mock(name, &block) : Mockery.instance.unnamed_mock(&block)
      stub.stub_everything
      stub.stubs(expectations)
      stub
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.