Last active
June 18, 2023 17:41
-
-
Save rickardlindberg/b69115374fb7ca473e68986f293e06de to your computer and use it in GitHub Desktop.
Python Mock does not detect changed signature
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
from unittest.mock import Mock | |
import unittest.mock | |
import doctest | |
class Calculator: | |
""" | |
>>> mock_display = Mock(Display) | |
>>> calculator = Calculator(mock_display) | |
>>> calculator.add(3) | |
>>> calculator.add(2) | |
>>> mock_display.show.assert_has_calls([ | |
... unittest.mock.call("Total = 3"), | |
... unittest.mock.call("Total = 5"), | |
... ]) | |
""" | |
def __init__(self, display): | |
self.display = display | |
self.total = 0 | |
def add(self, amount): | |
self.total += amount | |
self.display.show(f"Total = {self.total}") | |
class Display: | |
def show(self, text, color): | |
""" | |
>>> Display().show("hello", "red") | |
hello | |
""" | |
print(text) | |
if __name__ == "__main__": | |
doctest.testmod() | |
print("OK") | |
calculator = Calculator(Display()) | |
calculator.add(5) |
Author
rickardlindberg
commented
Jun 18, 2023
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment