diff options
| author | Samuel Freilich <sfreilich@google.com> | 2019-09-24 15:08:31 -0400 |
|---|---|---|
| committer | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2019-09-24 12:08:31 -0700 |
| commit | b5a7a4f0c20717a4c92c371583b5521b83f40f32 (patch) | |
| tree | f3f4df016736fca9503e1a8a6c28dc10222bbb43 /Lib/unittest/test/testmock/testmock.py | |
| parent | bb6bf7d342b4503a6227fd209fac934905b6a1aa (diff) | |
| download | cpython-git-b5a7a4f0c20717a4c92c371583b5521b83f40f32.tar.gz | |
bpo-36871: Handle spec errors in assert_has_calls (GH-16005)
The fix in PR 13261 handled the underlying issue about the spec for specific methods not being applied correctly, but it didn't fix the issue that was causing the misleading error message.
The code currently grabs a list of responses from _call_matcher (which may include exceptions). But it doesn't reach inside the list when checking if the result is an exception. This results in a misleading error message when one of the provided calls does not match the spec.
https://bugs.python.org/issue36871
Automerge-Triggered-By: @gpshead
Diffstat (limited to 'Lib/unittest/test/testmock/testmock.py')
| -rw-r--r-- | Lib/unittest/test/testmock/testmock.py | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/Lib/unittest/test/testmock/testmock.py b/Lib/unittest/test/testmock/testmock.py index ad67f98f87..88807d71cc 100644 --- a/Lib/unittest/test/testmock/testmock.py +++ b/Lib/unittest/test/testmock/testmock.py @@ -1435,6 +1435,25 @@ class MockTest(unittest.TestCase): mock.assert_has_calls(calls[:-1]) mock.assert_has_calls(calls[:-1], any_order=True) + def test_assert_has_calls_not_matching_spec_error(self): + def f(): pass + + mock = Mock(spec=f) + + with self.assertRaisesRegex( + AssertionError, + re.escape('Calls not found.\nExpected:')) as cm: + mock.assert_has_calls([call()]) + self.assertIsNone(cm.exception.__cause__) + + with self.assertRaisesRegex( + AssertionError, + re.escape('Error processing expected calls.\n' + "Errors: [None, TypeError('too many positional " + "arguments')]\n" + 'Expected:')) as cm: + mock.assert_has_calls([call(), call('wrong')]) + self.assertIsInstance(cm.exception.__cause__, TypeError) def test_assert_any_call(self): mock = Mock() |
