injectable.testing

Testing utilities to ease mocking injectables.

New in version 3.3.0.

Changed in version 3.4.0: Inclusion of the reset_injection_container() utility.

See also

The Injectable Mocking For Tests Example in the Usage Examples section shows how to use these utilities for mocking purposes.

See also

The Injection Container Resetting For Tests Example in the Usage Examples section shows how to use these utilities for clearing the injection container state.

injectable.testing.clear_injectables(dependency: Union[type, str], namespace: str = None) → Set[injectable.container.injectable.Injectable][source]

Utility function to clear all injectables registered for the dependency in a given namespace. Returns a set containing all cleared injectables.

Parameters:

Usage:

>>> from injectable.testing import clear_injectables
>>> clear_injectables("foo")

New in version 3.3.0.

injectable.testing.register_injectables(injectables: Collection[injectable.container.injectable.Injectable], klass: Optional[type] = None, qualifier: Optional[str] = None, namespace: str = 'DEFAULT_NAMESPACE', propagate: bool = False)[source]

Utility function to manually register injectables in a given namespace for the provided class and/or qualifier.

At least one of klass or qualifier parameters need to be defined. Otherwise a ValueError will be raised.

Parameters:
  • injectables – a collection of injectables to register.
  • klass – (optional) the class for which the injectables will be registered. This parameter is optional as long as qualifier is provided. Injectables registering won’t be propagated to base classes unless otherwise specified by the propagate parameter. Defaults to None.
  • qualifier – (optional) the qualifier for which the injectables will be registered. This parameter is optional as long as klass is provided. Defaults to None.
  • namespace – (optional) namespace in which the injectable will be registered. Defaults to injectable.constants.DEFAULT_NAMESPACE.
  • propagate – (optional) When True injectables registering will be propagated to base classes of klass recursively. Setting this parameter to True and not specifying the parameter klass will raise a ValueError. Defaults to False.

Usage:

>>> from injectable import Injectable
>>> from injectable.testing import register_injectables
>>> injectable = Injectable(constructor=lambda: 42)
>>> register_injectables({injectable}, qualifier="foo")

New in version 3.3.0.

injectable.testing.reset_injection_container()[source]

Utility function to reset the injection container, clearing all injectables registered from all namespaces and reseting the record for already scanned files.

Usage:

>>> from injectable.testing import reset_injection_container
>>> reset_injection_container()

New in version 3.4.0.