Skip to content

Commit f47699d

Browse files
committed
bpo-37555: Add tests checking every function using _call_matcher both with and without spec
1 parent d3522b1 commit f47699d

File tree

2 files changed

+43
-10
lines changed

2 files changed

+43
-10
lines changed

Lib/unittest/test/testmock/testasync.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
import inspect
33
import unittest
44

5-
from unittest.mock import (call, AsyncMock, patch, MagicMock, create_autospec,
6-
_AwaitEvent)
5+
from unittest.mock import (ANY, call, AsyncMock, patch, MagicMock,
6+
create_autospec, _AwaitEvent)
77

88

99
def tearDownModule():
@@ -599,6 +599,30 @@ def test_assert_has_awaits_no_order(self):
599599
asyncio.run(self._runnable_test('SomethingElse'))
600600
self.mock.assert_has_awaits(calls)
601601

602+
def test_awaits_asserts_with_any(self):
603+
class Foo:
604+
def __eq__(self, other): pass
605+
606+
asyncio.run(self._runnable_test(Foo(), 1))
607+
608+
self.mock.assert_has_awaits([call(ANY, 1)])
609+
self.mock.assert_awaited_with(ANY, 1)
610+
self.mock.assert_any_await(ANY, 1)
611+
612+
def test_awaits_asserts_with_spec_and_any(self):
613+
class Foo:
614+
def __eq__(self, other): pass
615+
616+
mock_with_spec = AsyncMock(spec=Foo)
617+
618+
async def _custom_mock_runnable_test(*args):
619+
await mock_with_spec(*args)
620+
621+
asyncio.run(_custom_mock_runnable_test(Foo(), 1))
622+
mock_with_spec.assert_has_awaits([call(ANY, 1)])
623+
mock_with_spec.assert_awaited_with(ANY, 1)
624+
mock_with_spec.assert_any_await(ANY, 1)
625+
602626
def test_assert_has_awaits_ordered(self):
603627
calls = [call('NormalFoo'), call('baz')]
604628
with self.assertRaises(AssertionError):

Lib/unittest/test/testmock/testhelpers.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,20 +63,29 @@ def __ne__(self, other): pass
6363
]
6464
self.assertEqual(expected, mock.mock_calls)
6565
self.assertEqual(mock.mock_calls, expected)
66-
mock.assert_has_calls(expected)
6766

68-
def test_assert_has_calls_with_any_and_spec_set(self):
67+
def test_any_no_spec(self):
6968
# This is a regression test for bpo-37555
70-
class Foo(object):
69+
class Foo:
7170
def __eq__(self, other): pass
72-
def __ne__(self, other): pass
7371

74-
mock = Mock(spec_set=Foo)
75-
expected = [call(ANY)]
76-
mock(Foo())
72+
mock = Mock()
73+
mock(Foo(), 1)
74+
mock.assert_has_calls([call(ANY, 1)])
75+
mock.assert_called_with(ANY, 1)
76+
mock.assert_any_call(ANY, 1)
77+
78+
def test_any_and_spec_set(self):
79+
# This is a regression test for bpo-37555
80+
class Foo:
81+
def __eq__(self, other): pass
7782

78-
mock.assert_has_calls(expected)
83+
mock = Mock(spec=Foo)
7984

85+
mock(Foo(), 1)
86+
mock.assert_has_calls([call(ANY, 1)])
87+
mock.assert_called_with(ANY, 1)
88+
mock.assert_any_call(ANY, 1)
8089

8190
class CallTest(unittest.TestCase):
8291

0 commit comments

Comments
 (0)