title: Mocking ftplib.FTP for unit testing Python code
body: <p>I don't know why I'm just not getting this, but I want to use mock in Python to test that my functions are calling functions in ftplib.FTP correctly. I've simplified everything down and still am not wrapping my head around how it works. Here is a simple example:</p>
  
  <pre><code>import unittest
  import ftplib
  from unittest.mock import patch
  
  def download_file(hostname, file_path, file_name):
      ftp = ftplib.FTP(hostname)
      ftp.login()
      ftp.cwd(file_path)
  
  class TestDownloader(unittest.TestCase):
  
      @patch('ftplib.FTP')
      def test_download_file(self, mock_ftp):
          download_file('ftp.server.local', 'pub/files', 'wanted_file.txt')
  
          mock_ftp.cwd.assert_called_with('pub/files')
  </code></pre>
  
  <p>When I run this, I get:</p>
  
  <pre><code>AssertionError: Expected call: cwd('pub/files')
  Not called
  </code></pre>
  
  <p>I know it must be using the mock object since that is a fake server name, and when run without patching, it throws a "socket.gaierror" exception.</p>
  
  <p>How do I get the actual object the fuction is running? The long term goal is not having the "download_file" function in the same file, but calling it from a separate module file.</p>