Skip to content

Commit

Permalink
Do not allow a bare thenAnswer()
Browse files Browse the repository at this point in the history
Because `when(m).foo().thenAnswer()` reads wrong when interpreted as
`return None` -- it reads like we *should* answer, (but what?), and
`None` is basically the `void` --, disallow its usage altogether for
clarity.

This is technically a breaking change.
  • Loading branch information
kaste committed Jan 24, 2025
1 parent b897058 commit fbb9a5f
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Release 1.5.5
- Improved behavior of the ad-hoc methods of mocks. (#92)
- Make the shortcut `when(mock).foo().thenReturn()` officially work and just assume
the user forgot the `None` as return value.
- Disallow the shortcut `when(mock).foo().thenAnswer()` as it reads odd.



Expand Down
2 changes: 2 additions & 0 deletions mockito/invocation.py
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,8 @@ def thenRaise(self, *exceptions):
return self

def thenAnswer(self, *callables):
if not callables:
raise TypeError("No answer function provided")
for callable in callables:
answer = callable
if self.discard_first_arg:
Expand Down
6 changes: 6 additions & 0 deletions tests/when_interface_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ def testAssumeReturnNoneIfOmitted(self):
assert dog.bark() is None
assert dog.bark() == 42

def testRaiseIfAnswerIsOmitted(self):
dog = Dog()
with pytest.raises(TypeError) as exc:
when(dog).bark().thenAnswer()
assert str(exc.value) == "No answer function provided"

def testAssumeRaiseExceptionIfOmitted(self):
dog = Dog()
when(dog).bark().thenRaise().thenReturn(42)
Expand Down

0 comments on commit fbb9a5f

Please sign in to comment.