diff --git a/connect/client/testing/fixtures.py b/connect/client/testing/fixtures.py index 5558d5a..70bdaa9 100644 --- a/connect/client/testing/fixtures.py +++ b/connect/client/testing/fixtures.py @@ -17,6 +17,7 @@ def client_mocker_factory(request): mocker = None def _wrapper(base_url='https://example.org/public/v1', exclude=None): + nonlocal mocker mocker = ConnectClientMocker(base_url, exclude=exclude) mocker.start() return mocker diff --git a/connect/client/testing/fluent.py b/connect/client/testing/fluent.py index 37c5168..1e11f8b 100644 --- a/connect/client/testing/fluent.py +++ b/connect/client/testing/fluent.py @@ -153,8 +153,10 @@ def start(self): _mocker.start() def reset(self, success=True): - _mocker.stop(allow_assert=success) - _mocker.reset() + try: + _mocker.stop(allow_assert=success) + finally: + _mocker.reset() def __enter__(self): self.start() diff --git a/tests/client/test_fixtures.py b/tests/client/test_fixtures.py index 73d0d07..17a110f 100644 --- a/tests/client/test_fixtures.py +++ b/tests/client/test_fixtures.py @@ -1,6 +1,7 @@ +import pytest import requests -from connect.client import ConnectClient +from connect.client import ClientError, ConnectClient def test_client_mocker_factory(client_mocker_factory): @@ -11,6 +12,12 @@ def test_client_mocker_factory(client_mocker_factory): assert client.products.create(payload={}) == {'id': 'PRD-000'} +def test_client_mocker_factory_finalizer(): + client = ConnectClient('api_key', endpoint='http://example.com') + with pytest.raises(ClientError): + client.products.create(payload={}) + + def test_client_mocker_factory_default_base_url(client_mocker_factory): mocker = client_mocker_factory() mocker.products.create(return_value={'id': 'PRD-000'}) diff --git a/tests/client/test_testing.py b/tests/client/test_testing.py index d24fc3f..fe700df 100644 --- a/tests/client/test_testing.py +++ b/tests/client/test_testing.py @@ -395,3 +395,15 @@ def test_exclude(mocker, exclude): def test_get_requests_mocker(): assert get_requests_mocker() == _mocker + + +def test_mocker_reset(mocker): + mocker.patch( + 'connect.client.testing.fluent._mocker.stop', + side_effect=Exception('error'), + ) + mocked_reset = mocker.patch('connect.client.testing.fluent._mocker.reset') + mocker = ConnectClientMocker('http://localhost') + with pytest.raises(Exception): + mocker.reset() + mocked_reset.assert_called_once()