In the following question the function that uses the ftplib is defined in the same file, which makes it trivial to patch ('ftplib.FTP')
Mocking ftplib.FTP for unit testing Python code
My question is: How should I proceed if, in my test, I would like to make an instance of a class (let's call it 'A') that uses the ftplib somewhere (ie: the class A has an instance of a class B and B has an FTP object which calls connect()) ?
import unittest
from mock import patch
class TestClass(unittest.TestCase):
@patch(???)
def test_1(self, mock_ftp_constructor):
mock_ftp = mock_ftp_constructor.return_value
a = A()
self.assertTrue(mock_ftp.connect.called)
if __name__ == "__main__":
unittest.main()
If I use the solution given in the other question, I find that the ftplib is called instead of the mock. How could I know the correct path of ftplib.FTP ?