title: Python mocking elasticsearch connection generator `with` statement
body: <p>I would like to test a class with a function that has <code>with</code> statement in it:</p>
  
  <pre><code>func_to_test():
     ....
     with self.__elastic_generator.get() as es:
        print 'about to use the es connection....'
  </code></pre>
  
  <p>So I mocked the elstic_generator, and I mocked it <code>get</code> function when creating the class tested:</p>
  
  <pre><code>elastic_gen = mock.Mock()
  elstic_gen.get = mock.Mock()
  elastic_gen.get.side_effect = ['mocked elastic connection']
  tested_class = TestedClass(elastic_gen)
  tested_class.func_to_test()
  </code></pre>
  
  <p>But for some reason that doesn't work when using the <code>with</code> statement.
  However, if getting the connection without using <code>with</code>, like this:</p>
  
  <p><code>x = self.__elastic_generator.get()</code></p>
  
  <p>Then it works fine, and I get <code>x = 'mocked elastic connection'</code>.</p>
  
  <p>So I guess the problem is related to some more functions calls being made when using the <code>with</code> and that I don't mock these functions.</p>
  
  <p>Can someone please explain what happens under the hood and what else should I mock in order to be able to test it with the <code>with</code> statement?</p>
  
  <p>Thanks.</p>