# Module: Mocha::Hooks

Included in:
  API

Defined in:
  lib/mocha/hooks.rb

## Overview

Integration hooks for test library authors.

The methods in this module should be called from test libraries wishing to integrate with Mocha.

This module is provided as part of the `Mocha::API` module and is therefore part of the public API, but should only be used by authors of test libraries and not by typical “users” of Mocha.

Integration with Test::Unit and Minitest are provided as part of Mocha, because they are (or were once) part of the Ruby standard library. Integration with other test libraries is not provided as part of Mocha, but is supported by means of the methods in this module.

See the code in the `Adapter` modules for examples of how to use the methods in this module. `Mocha::ExpectationErrorFactory` may be used if you want `Mocha` to raise a different type of exception.

See Also:

- Integration::TestUnit::Adapter
- Integration::Minitest::Adapter
- ExpectationErrorFactory
- API

## Instance Method Summary collapse

- #mocha_setup(assertion_counter = NullAssertionCounter.new) ⇒ Object

  Prepares Mocha before a test (only for use by authors of test libraries).

- #mocha_teardown(origin = mocha_test_name) ⇒ Object

  Resets Mocha after a test (only for use by authors of test libraries).

- #mocha_test_name ⇒ Object

  Returns a string representing the unit test name, to be included in some Mocha to help track down potential bugs.

- #mocha_verify ⇒ Object

  Verifies that all mock expectations have been met (only for use by authors of test libraries).

## Instance Method Details

### #mocha_setup(assertion_counter = NullAssertionCounter.new) ⇒ `Object`

Prepares Mocha before a test (only for use by authors of test libraries).

This method should be called before each individual test starts (including before any “setup” code).

    29
    30
    31

    # File 'lib/mocha/hooks.rb', line 29

    def mocha_setup(assertion_counter = NullAssertionCounter.new)
      Mockery.setup(assertion_counter)
    end

### #mocha_teardown(origin = mocha_test_name) ⇒ `Object`

Resets Mocha after a test (only for use by authors of test libraries).

This method should be called after each individual test has finished (including after any “teardown” code).

    45
    46
    47

    # File 'lib/mocha/hooks.rb', line 45

    def mocha_teardown(origin = mocha_test_name)
      Mockery.teardown(origin)
    end

### #mocha_test_name ⇒ `Object`

Returns a string representing the unit test name, to be included in some Mocha to help track down potential bugs.

    51
    52
    53

    # File 'lib/mocha/hooks.rb', line 51

    def mocha_test_name
      nil
    end

### #mocha_verify ⇒ `Object`

Verifies that all mock expectations have been met (only for use by authors of test libraries).

This is equivalent to a series of “assertions”.

This method should be called at the end of each individual test, before it has been determined whether or not the test has passed.

    38
    39
    40

    # File 'lib/mocha/hooks.rb', line 38

    def mocha_verify
      Mockery.verify
    end