Skip to content
This repository has been archived by the owner on Jan 14, 2024. It is now read-only.

Commit

Permalink
Monkey patch assert_not_called into unittest.mock.Mock
Browse files Browse the repository at this point in the history
  • Loading branch information
horazont committed May 1, 2017
1 parent d553617 commit a96c035
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
13 changes: 13 additions & 0 deletions aioxmpp/testutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -779,3 +779,16 @@ def error_future(self):
fut = asyncio.Future()
self._error_futures.append(fut)
return fut


if not hasattr(unittest.mock.Mock, "assert_not_called"):
def _assert_not_called(m):
if any(not call[0] for call in m.mock_calls):
raise AssertionError(
"expected {!r} to not have been called. "
"Called {} times".format(
m._mock_name or 'mock',
m.call_count)
)
unittest.mock.Mock.assert_not_called = _assert_not_called
del _assert_not_called
11 changes: 11 additions & 0 deletions tests/test_testutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1050,3 +1050,14 @@ def test_set_delay(self):
],
m.mock_calls
)


class TestMockMonkeyPatches(unittest.TestCase):
def test_assert_not_called_works(self):
m = unittest.mock.Mock()
m.assert_not_called()
self.assertFalse(m.mock_calls)

m()
with self.assertRaises(AssertionError):
m.assert_not_called()

0 comments on commit a96c035

Please sign in to comment.