-
-
Notifications
You must be signed in to change notification settings - Fork 150
Closed
Description
Hello,
Since #173 was merged (and pytest-mock 1.13.0 released), mocker.spy
can't be called successfully once a spied function raised an exception.
The issue is that mocker.spy
relies on a side-effect to wrap all the calls:
pytest-mock/src/pytest_mock/plugin.py
Line 125 in 7bddcd5
result = self.patch.object(obj, name, side_effect=wrapper, autospec=autospec) |
But now that we assign a new side-effect after an exception was raised, the spy will always raise the exception instead of calling the wrapper.
Here is a test case to reproduce the issue:
def test_spy_side_effect(mocker):
class Foo:
def bar(self, arg):
if arg > 0:
return arg
raise RuntimeError("I'm an error")
foo = Foo()
mocker.spy(foo, 'bar')
assert foo.bar(42) == 42
foo.bar.assert_called_with(42)
with pytest.raises(RuntimeError) as exc_info:
foo.bar(-1)
assert str(exc_info.value) == "I'm an error"
foo.bar.assert_called_with(-1)
# with pytest-mock 1.13.0 this will raise a RuntimeError instead of returning 21
assert foo.bar(21) == 21
foo.bar.assert_called_with(21)
A possible solution would be to assign the exception to result.return_value
instead of result.side_effect
as proposed initially in #173. However I understand that this is not perfect either.
Metadata
Metadata
Assignees
Labels
No labels