mocksafe.mock

mocksafe.mock(spec: type[T], name: str | None = None) T

Creates a mock of the given spec.

Parameters:
  • spec (type[T]) – the specification (class/protocol/type) to be mocked of generic type T

  • name (str, optional) – optional custom name to help identify the mock object in str representations

Returns:

a mock object but typed with the generic type T

Return type:

T

Example:
>>> from random import Random
>>> mock_random: Random = mock(Random)
>>> mock_random
SafeMock(<class 'random.Random'>)

To stub a mocked method see:

mocksafe.when()

To assert calls made to a mocked method see:

mocksafe.that()

To retrieve calls that were made to a mocked method see:

mocksafe.spy()