pyapp icon indicating copy to clipboard operation
pyapp copied to clipboard

Support patching of injection registry for testing

Open timsavage opened this issue 2 years ago • 0 comments

Similar to how settings can be patched for testing, the injection registry should also allow Mocks to be injected for test purposes.

With an interface like:

from unittest.mock import Mock, call
from pyapp import injection

def test_function_with_injected_type():
    with injection.default_registry.modify() as patch:
        mocked_type = Mock(spec_set=MyType)

        # With a helper factory generator
        patch.mock_factory(MyType, mocked_type)

        # With a direct inserted item
        patch[MyType] = Mock(return_value=mocked_type)

        function_with_injected_type()

    assert mocked_type.method.mock_calls == [
      call()
    ]

timsavage avatar Jul 20 '22 02:07 timsavage