Created
August 8, 2021 13:43
-
-
Save elnygren/3ef57d7ff4fb4a2e7818fb389518255e to your computer and use it in GitHub Desktop.
Testing DI enabled Python code.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pytest | |
from unittest.mock import MagicMock | |
@pytest.fixture() | |
def patch_user_service(): | |
""" | |
reusable patcher, | |
handy when there are lots of test cases | |
""" | |
user_connector = MagicMock() | |
org_connector = MagicMock() | |
service = UserService(user_connector, org_connector) | |
return user_connector, org_connector | |
def test_get_user_with_org(patch_user_service): | |
""" | |
test case that uses our patch_user_service | |
we get access to the mocks can can check that they | |
are called correctly etc. | |
we could also alter their behavior before calling | |
our service | |
""" | |
m_user, m_org = patch_user_service | |
result = service.get_user_with_org('test-ID') | |
assert m_user.get_user.called | |
assert m_org.get_org.called | |
assert result | |
assert 'user' in result | |
assert 'org' in result | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment