mirror of
https://github.com/python/cpython.git
synced 2025-07-24 03:35:53 +00:00
Restrict use of Mock objects as specs (GH-31090)
Follow-on to https://github.com/python/cpython/pull/25326 This covers cases where mock objects are passed directly to spec.
This commit is contained in:
parent
8726067ace
commit
6394e981ad
4 changed files with 20 additions and 3 deletions
|
@ -226,6 +226,14 @@ class MockTest(unittest.TestCase):
|
|||
with self.assertRaisesRegex(InvalidSpecError,
|
||||
"Cannot spec attr 'B' as the spec_set "):
|
||||
mock.patch.object(A, 'B', spec_set=A.B).start()
|
||||
with self.assertRaisesRegex(InvalidSpecError,
|
||||
"Cannot spec attr 'B' as the spec_set "):
|
||||
mock.patch.object(A, 'B', spec_set=A.B).start()
|
||||
with self.assertRaisesRegex(InvalidSpecError, "Cannot spec a Mock object."):
|
||||
mock.Mock(A.B)
|
||||
with mock.patch('builtins.open', mock.mock_open()):
|
||||
mock.mock_open() # should still be valid with open() mocked
|
||||
|
||||
|
||||
def test_reset_mock(self):
|
||||
parent = Mock()
|
||||
|
|
|
@ -130,8 +130,8 @@ class WithTest(unittest.TestCase):
|
|||
|
||||
c = C()
|
||||
|
||||
with patch.object(c, 'f', autospec=True) as patch1:
|
||||
with patch.object(c, 'f', autospec=True) as patch2:
|
||||
with patch.object(c, 'f') as patch1:
|
||||
with patch.object(c, 'f') as patch2:
|
||||
c.f()
|
||||
self.assertEqual(patch2.call_count, 1)
|
||||
self.assertEqual(patch1.call_count, 0)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue