# Module: Mocha::API

Includes:
  Hooks, ParameterMatchers

Included in:
  Integration::MiniTest::Adapter, Integration::TestUnit::Adapter

Defined in:
  lib/mocha/api.rb

## Overview

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

## Instance Method Summary collapse

- #mock(*arguments) { ... } ⇒ Mock

  Builds a new mock object.

- #sequence(name) ⇒ Sequence

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

- #states(name) ⇒ StateMachine

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

- #stub(*arguments) { ... } ⇒ Mock

  Builds a new mock object.

- #stub_everything(*arguments) { ... } ⇒ Mock

  Builds a mock object that accepts calls to any method.

### Methods included from Hooks

#mocha_setup, #mocha_teardown, #mocha_verify

### Methods included from ParameterMatchers

#Not, #all_of, #any_of, #any_parameters, #anything, #equals, #equivalent_uri, #has_entries, #has_entry, #has_equivalent_query_string, #has_key, #has_value, #includes, #instance_of, #is_a, #kind_of, #optionally, #regexp_matches, #responds_with, #yaml_equivalent

## Instance Method Details

### #mock(name, &block) ⇒ `Mock` #mock(expected_methods_vs_return_values = {}, &block) ⇒ `Mock` #mock(name, expected_methods_vs_return_values = {}, &block) ⇒ `Mock`

Builds a new mock object

Examples:

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

Using the optional block to setup expectations & stubbed methods [deprecated].

    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

Parameters:

- name (`String`) —

  identifies mock object in error messages.

- expected_methods_vs_return_values (`Hash`) —

  expected method name symbols as keys and corresponding return values as values - these expectations are setup as if Mock#expects were called multiple times.

Yields:

- optional block to be evaluated in the context of the mock object instance, giving an alternative way to setup stubbed methods.

- note that the block is evaulated by calling Mock#instance_eval and so things like instance variables declared in the test will not be available within the block.

- deprecated: use Object#tap or define stubs/expectations with an explicit receiver instead.

Returns:

- (`Mock`) —

  a new mock object

    52
    53
    54
    55
    56
    57
    58
    # 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) ⇒ `Sequence`

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.

Examples:

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

Returns:

- (`Sequence`) —

  a new sequence

See Also:

- Expectation#in_sequence

    144
    145
    146
    # File 'lib/mocha/api.rb', line 144

    def sequence(name)
      Sequence.new(name)
    end

### #states(name) ⇒ `StateMachine`

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.

Examples:

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

Returns:

- (`StateMachine`) —

  a new state machine

See Also:

- Expectation#then
- Expectation#when
- StateMachine

    174
    175
    176
    # File 'lib/mocha/api.rb', line 174

    def states(name)
      Mockery.instance.new_state_machine(name)
    end

### #stub(name, &block) ⇒ `Mock` #stub(stubbed_methods_vs_return_values = {}, &block) ⇒ `Mock` #stub(name, stubbed_methods_vs_return_values = {}, &block) ⇒ `Mock`

Builds a new mock object

Examples:

Using stubbed_methods_vs_return_values Hash to setup stubbed methods.

    def test_motor_starts_and_stops
      motor = stub('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

Using the optional block to setup expectations & stubbed methods [deprecated].

    def test_motor_starts_and_stops
      motor = stub('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

Parameters:

- name (`String`) —

  identifies mock object in error messages.

- stubbed_methods_vs_return_values (`Hash`) —

  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.

Yields:

- optional block to be evaluated in the context of the mock object instance, giving an alternative way to setup stubbed methods.

- note that the block is evaulated by calling Mock#instance_eval and so things like instance variables declared in the test will not be available within the block.

- deprecated: use Object#tap or define stubs/expectations with an explicit receiver instead.

Returns:

- (`Mock`) —

  a new mock object

    91
    92
    93
    94
    95
    96
    97
    # File 'lib/mocha/api.rb', line 91

    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(name, &block) ⇒ `Mock` #stub_everything(stubbed_methods_vs_return_values = {}, &block) ⇒ `Mock` #stub_everything(name, stubbed_methods_vs_return_values = {}, &block) ⇒ `Mock`

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

Examples:

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

Parameters:

- name (`String`) —

  identifies mock object in error messages.

- stubbed_methods_vs_return_values (`Hash`) —

  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.

Yields:

- optional block to be evaluated in the context of the mock object instance, giving an alternative way to setup stubbed methods.

- note that the block is evaulated by calling Mock#instance_eval and so things like instance variables declared in the test will not be available within the block.

- deprecated: use Object#tap or define stubs/expectations with an explicit receiver instead.

Returns:

- (`Mock`) —

  a new mock object

    119
    120
    121
    122
    123
    124
    125
    126
    # File 'lib/mocha/api.rb', line 119

    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