Skip to content

Instantly share code, notes, and snippets.

@rickardlindberg
Last active June 18, 2023 17:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rickardlindberg/b69115374fb7ca473e68986f293e06de to your computer and use it in GitHub Desktop.
Save rickardlindberg/b69115374fb7ca473e68986f293e06de to your computer and use it in GitHub Desktop.
Python Mock does not detect changed signature
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)
@rickardlindberg
Copy link
Author

rickardlindberg commented Jun 18, 2023

$ python test.py 
OK
Traceback (most recent call last):
  File "/tmp/test.py", line 39, in <module>
    calculator.add(5)
  File "/tmp/test.py", line 24, in add
    self.display.show(f"Total = {self.total}")
TypeError: show() missing 1 required positional argument: 'color'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment