injectable.testing¶
Testing utilities to ease mocking injectables.
Added 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: type | str, namespace: str = None) Set[Injectable][source]¶
Utility function to clear all injectables registered for the dependency in a given namespace. Returns a set containing all cleared injectables.
- Parameters:
dependency – class or qualifier of the dependency.
namespace – (optional) namespace in which the injectable will be registered. Defaults to
injectable.constants.DEFAULT_NAMESPACE.
Usage:
>>> from injectable.testing import clear_injectables >>> clear_injectables("foo")
Added in version 3.3.0.
- injectable.testing.register_injectables(injectables: Collection[Injectable], klass: type | None = None, qualifier: str | None = 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
klassorqualifierparameters need to be defined. Otherwise aValueErrorwill 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
qualifieris provided. Injectables registering won’t be propagated to base classes unless otherwise specified by thepropagateparameter. Defaults to None.qualifier – (optional) the qualifier for which the injectables will be registered. This parameter is optional as long as
klassis 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
klassrecursively. Setting this parameter to True and not specifying the parameterklasswill raise aValueError. Defaults to False.
Usage:
>>> from injectable import Injectable >>> from injectable.testing import register_injectables >>> injectable = Injectable(constructor=lambda: 42) >>> register_injectables({injectable}, qualifier="foo")
Added 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()
Added in version 3.4.0.