title: PicklingError when using mock to check function is called
body: <p>I'm having trouble testing that a method is called using <code>mock</code> -- as a simple example, let's say that the method is <code>os.getcwd</code>. I want to test that my own function, <code>pickle_wdir</code>, is calling <code>os.getcwd</code> as intended. However, the function I am testing pickles the value returned by <code>os.getcwd</code>, which results in an error.</p>
  
  <p>Here's a simple example to reproduce the error. </p>
  
  <p><code>os_ex.py</code>:</p>
  
  <pre><code>import os
  import pickle
  
  def pickle_wdir(filename):
      dir = os.getcwd()
      with open(filename, 'wb') as handle:
          pickle.dump(dir, handle)
  </code></pre>
  
  <p><code>test_os_ex.py</code>:</p>
  
  <pre><code>from unittest import TestCase
  from unittest.mock import patch
  from os_ex import pickle_wdir
  
  
  class TestPickleWdir(TestCase):
      def test_os_called(self):
          fname = 'dir.pickle'
          with patch('os_ex.os') as mocked_obj:
              pickle_wdir(fname)
              mocked_obj.getcwd.assert_called()
  </code></pre>
  
  <p>The error message returned is </p>
  
  <p><code>_pickle.PicklingError: Can't pickle &lt;class 'unittest.mock.MagicMock'&gt;: it's not the same object as unittest.mock.MagicMock</code>.</p>
  
  <p>How can I test that <code>os.getcwd</code> is called without getting this <code>PicklingError</code>?</p>