خرید بک لینک

Vote count: 0

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 coect()) ?

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.coect.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 ?

asked 12 secs ago

برچسب: نویسنده: استخدام کار تاريخ: سه شنبه 26 ارديبهشت 1396 ساعت: 20:49

صفحه بندی