title: Python mocking open and check for close
body: <p>I am trying to mock open and want to check if close gets called at least once</p>
  
  <pre><code>class MyObject():
      def __init__(self,path):
          fp = open(path)
          self.file_list = []
          for line in fp:
              self.file_list.append(line.strip())
          fp.close()   
  
  
  
  def testsimpleFile():
      fake_file = io.StringIO("data.csv\ndata2.csv")
      with patch("builtins.open",return_value=fake_file,create=True) as mock_file:
          f = MyObject("path/to/open/test.f")
          mock_file.assert_called_once_with("/path/to/open/test.f")
          golden_list = ["data.csv","data2.csv"]
          assert f.file_list == golden_list
  </code></pre>
  
  <p>This is my working testcode until now and now i want to additionally check if the the close method was called i tried to add </p>
  
  <p><code>mock_file.close.assert_called_once()</code></p>
  
  <p>and</p>
  
  <pre><code>mock_file.fake_file.close.assert_called_once()
  </code></pre>
  
  <p>but both will not catch the method call.</p>