title: How do I mock an open(...).write() without getting a 'No such file or directory' error?
body: <p>I've based my solution on:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/1289894/how-do-i-mock-an-open-used-in-a-with-statement-using-the-mock-framework-in-pyth">How do I mock an open used in a with statement (using the Mock framework in Python)?</a>,  </li>
  <li><a href="https://stackoverflow.com/questions/31705057/attributeerror-module-main-from-does-not-have-the-attribute-open">AttributeError: &lt;module &#39;__main__&#39; from [..] does not have the attribute &#39;open&#39;</a>, </li>
  <li><a href="http://www.voidspace.org.uk/python/mock/helpers.html#mock.mock_open" rel="noreferrer">http://www.voidspace.org.uk/python/mock/helpers.html#mock.mock_open</a></li>
  </ul>
  
  <p>I have a class, which I can instantiate, which writes to a file. I'm trying to test it, but I'm having problems mocking <code>open()</code>. I'm using the following as the smallest piece of code, which can </p>
  
  <pre><code>import os
  import unittest
  from unittest.mock import mock_open, patch
  
  __author__ = 'drews'
  
  
  class MockPathExists(object):
      def __init__(self, return_value):
          self.received_args = None
          self.return_value = return_value
  
      def __call__(self, *args, **kwargs):
          self.received_args = args
          return self.return_value
  
  
  class WriteData:
      def __init__(self, dir, name='World'):
          self.name = name
          self.dir = dir
  
      def dump(self):
          if os.path.exists(self.dir):
              with open('{0}/output.text'.format(self.dir), 'w+') as fp:
                  fp.write('Hello, {0}!'.format(self.name))
  
  
  class TestListWindowsPasswords(unittest.TestCase):
      def setUp(self):
          self._orig_pathexists = os.path.exists
          os.path.exists = MockPathExists(True)
  
      def test_dump(self):
          m = mock_open()
          with patch.object(WriteData, 'open', m, create=True):
              data_writer = WriteData(
                  dir='/my/path/not/exists',
                  name='Foo'
              )
              data_writer.dump()
  
          self.assertEqual(os.path.exists.received_args[0], '/my/path/not/exists/output.text')
          m.assert_called_once_with('/my/path/not/exists/output.text', 'w+')
          handle = m()
          handle.write.assert_called_once_with('Hello, Foo!')
  
  
  
      def tearDown(self):
          os.path.exists = self._orig_pathexists
  </code></pre>
  
  <p>When I run this, I get the following error:</p>
  
  <pre><code>Error
  Traceback (most recent call last):
    File "/Users/drews/Development/tool/tests/test_mockopen.py", line 41, in test_dump
      data_writer.dump()
    File "/Users/drews/Development/tool/tests/test_mockopen.py", line 25, in dump
      with open('{0}/output.text'.format(self.dir), 'w+') as fp:
  FileNotFoundError: [Errno 2] No such file or directory: '/my/path/not/exists/output.text'
  </code></pre>
  
  <p>How can I mock open(), so that it just returns a file_pointer, and doesn't try to interact with the file system at all?</p>