You can patch zipfile.Zipfile
with autospec=True
:
If you set
autospec=True
then the mock with be created with a spec from the object being replaced. All attributes of the mock will also have the spec of the corresponding attribute of the object being replaced. Methods and functions being mocked will have their arguments checked and will raise aTypeError
if they are called with the wrong signature. For mocks replacing a class, their return value (the ‘instance’) will have the same spec as the class.
The following test will fail due to AttributeError: Mock object has no attribute 'namlist'
:
from unittest import TestCase
from mock import patch
class MyTestCase(TestCase):
@patch.object(ZipFile, 'namelist', autospec=True)
def test_my_method(self, mocked_zipfile):
mocked_zipfile.namlist.return_value = [ 'one.txt', 'two.txt' ]
Hope that helps.