title: Mocking functions from object created by context manager
body: <p>I have the following module that I am trying to write unit tests for.</p>
  
  <pre><code>import myModuleWithCtxMgr
  
  def myFunc(arg1):
      with myModuleWithCtxMgr.ctxMgr() as ctxMgr:
          result = ctxMgr.someFunc()
  
      if result:
          return True, result
      return False, None
  </code></pre>
  
  <p>The unit tests I'm working on looks like this.</p>
  
  <pre><code>import mock
  import unittest
  import myModule as myModule
  
  class MyUnitTests(unittest.TestCase):
  
      @mock.patch("myModuleWithCtxMgr.ctxMgr")
      def testMyFunc(self, mockFunc):
          mockReturn = mock.MagicMock()
          mockReturn.someFunc = mock.Mock(return_value="val")
          mockFunc.return_value = mockReturn
          result = myModule.myFunc("arg")
  
          self.assertEqual(result, (True, "val"))
  </code></pre>
  
  <p>The test is failing because result[0] = magicMock() and not the return value (I thought) I configured.</p>
  
  <p>I've tried a few different variations of the test but I can't seem to be able to mock the return value of ctxMgr.someFunc(). Does anyone know how I might accomplish this?</p>
  
  <p>Thanks!</p>