title: Mocking Functions Using Python Mock
body: <p>I am trying to <code>Mock</code> a function (that returns some external content) using the python <a href="https://docs.python.org/3/library/unittest.mock.html" rel="noreferrer">mock module</a>.</p>
  
  <p>I'm having some trouble mocking functions that are imported into a module.</p>
  
  <p>For example, in <code>util.py</code> I have </p>
  
  <pre><code>def get_content():
    return "stuff"
  </code></pre>
  
  <p>I want to mock <code>util.get_content</code> so that it returns something else.</p>
  
  <p>I am trying this:</p>
  
  <pre><code>util.get_content=Mock(return_value="mocked stuff")
  </code></pre>
  
  <p>If <code>get_content</code> gets invoked inside another module, it never actually seems to return the mocked object. Am I missing something in terms of how to use <code>Mock</code>?</p>
  
  <p>Note that if I invoke the following, things work correctly:</p>
  
  <pre><code>&gt;&gt;&gt; util.get_content=Mock(return_value="mocked stuff")
  &gt;&gt;&gt; util.get_content()
  "mocked stuff"
  </code></pre>
  
  <p>However, if <code>get_content</code> is called from inside another module, it invokes the original function instead of the mocked version:</p>
  
  <pre><code>&gt;&gt;&gt; from mymodule import MyObj
  &gt;&gt;&gt; util.get_content=Mock(return_value="mocked stuff")
  &gt;&gt;&gt; m=MyObj()
  &gt;&gt;&gt; m.func()
  "stuff"
  </code></pre>
  
  <p>Contents of <code>mymodule.py</code></p>
  
  <pre><code>from util import get_content
  
  class MyObj:    
      def func():
          get_content()
  </code></pre>
  
  <p>So I guess my question is - how do I get invoke the Mocked version of a function from inside a module that I call?</p>
  
  <p>It appears that the <code>from module import function</code> may be to blame here, in that it doesn't point to the Mocked function.</p>