I am trying to use a method as a spec_set
on a MagicMock
.
https://stackoverflow.com/a/25323517/11163122 provides a nice example of an unexpected call signature resulting in an Exception
. Unfortunately, I can't get it to work out for a method.
How can I get the below code snippet to error out, given the calls don't match the spec?
from unittest.mock import MagicMock
class Foo:
def bar(self) -> None:
"""Some method I want to use as a spec."""
mocking_foo_bar_1 = MagicMock(name="with spec", spec=Foo.bar)
mocking_foo_bar_2 = MagicMock(name="with spec_set", spec_set=Foo.bar)
# These aren't raising, even though they don't match the spec
mocking_foo_bar_1("this", "should", "raise")
mocking_foo_bar_2("this", "also", "should", "raise")
I am using Python 3.8+.