Last active
November 4, 2024 06:36
-
-
Save r-leyshon/817e19438380eb9df638dfb1cd4c242e to your computer and use it in GitHub Desktop.
Mock with fixture
This file contains hidden or 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
from datetime import datetime | |
def get_poem_line_for_day(): | |
"""Returns the line of the poem based on the current day of the week.""" | |
day_of_week = datetime.today().strftime('%A') | |
POEM = { | |
"Monday": "Monday's child is fair of face", | |
"Tuesday": "Tuesday's child is full of grace", | |
"Wednesday": "Wednesday's child is full of woe", | |
"Thursday": "Thursday's child has far to go", | |
"Friday": "Friday's child is loving and giving", | |
"Saturday": "Saturday's child works hard for his living", | |
"Sunday": "And the child that is born on the Sabbath day is bonny and blithe, and good and gay", | |
} | |
return POEM.get(day_of_week, "Unknown day") |
This file contains hidden or 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
from unittest import mock | |
import pytest | |
import poem | |
# setup ---------------------------------------------------------------- | |
POEM = { | |
"Monday": "Monday's child is fair of face", | |
"Tuesday": "Tuesday's child is full of grace", | |
"Wednesday": "Wednesday's child is full of woe", | |
"Thursday": "Thursday's child has far to go", | |
"Friday": "Friday's child is loving and giving", | |
"Saturday": "Saturday's child works hard for his living", | |
"Sunday": "And the child that is born on the Sabbath day is bonny and blithe, and good and gay", | |
} | |
# local mock ---------------------------------------------------------- | |
@mock.patch("poem.get_poem_line_for_day") | |
def test_poem_line_forever_thursday(patched_poem): | |
"""Uses immediate instantiation""" | |
def mock_poem(day, poem=POEM): | |
return poem[day] | |
patched_poem.return_value = mock_poem(day="Thursday") | |
result = poem.get_poem_line_for_day() | |
assert result == "Thursday's child has far to go" | |
# fixture mock --------------------------------------------------------- | |
@pytest.fixture(scope="function") | |
def mock_poem_line_factory(): | |
"""Factory function that mocks expected return values.""" | |
def _get_poem_line(day_name: str, poem: dict = POEM) -> str: | |
return poem[day_name] | |
return _get_poem_line | |
@mock.patch("poem.get_poem_line_for_day") | |
def test_poem_line_any_day_we_like(patched_poem, mock_poem_line_factory): | |
"""Uses deferred instantiation.""" | |
patched_poem.return_value = mock_poem_line_factory(day_name="Wednesday") | |
result = poem.get_poem_line_for_day() | |
assert result == "Wednesday's child is full of woe" | |
patched_poem.return_value = mock_poem_line_factory(day_name="Friday") | |
result = poem.get_poem_line_for_day() | |
assert result == "Friday's child is loving and giving" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment