Skip to content

Document method to patch hub function

Given code which calls a function on the hub, what is the suggested mechanism to patch that function's output?

A test which includes:

with patch('hub.validator.assertions.assertThing.check') as mock:
        instance = mock.return_value
        instance.method.return_value = 'the result'
        result = check._assert_block(hub, test_name, test_dict)

Fails with

hub = <pop.hub.Hub object at 0x7f0727c29dd8>

    def test_assert_block(hub):
        mock_hub = hub.pop.testing.mock_hub()
        test_name = 'a test'
        test_dict = {"assertion": "assertThing", "expected_return": "stuff"}
        hub.validator.RUNS = {test_name: {"execution_output": "stuff"}}
>       with patch('hub.validator.assertions.assertThing.check') as mock:

tests/unit/test_check.py:53: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
/root/.local/share/virtualenvs/mypop-mGqZu586/lib/python3.7/site-packages/mock/mock.py:1421: in __enter__
    self.target = self.getter()
/root/.local/share/virtualenvs/mypop-mGqZu586/lib/python3.7/site-packages/mock/mock.py:1595: in <lambda>
    getter = lambda: _importer(target)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

target = 'hub.validator.assertions.assertThing'

    def _importer(target):
        components = target.split('.')
        import_path = components.pop(0)
>       thing = __import__(import_path)
E       ModuleNotFoundError: No module named 'hub'

/root/.local/share/virtualenvs/mypop-mGqZu586/lib/python3.7/site-packages/mock/mock.py:1245: ModuleNotFoundError