title: How to mock a decorated function
body: <p>For testing reasons, I need to be able to mock the inner/original function of a decorated one which is used somewhere else:</p>
  
  <p>In mydecorator.py:</p>
  
  <pre><code>def my_decorator(f):
      def wrapped_f():
          print "decorated"
          f()
      return wrapped_f
  
  
  @my_decorator
  def function_to_be_mocked():
      print 'original'
  
  
  def function_to_be_mocked_undecorated():
      print 'original'
  
  
  def run_decorated():
      function_to_be_mocked()
  
  
  def run_undecorated():
      decorated_funtion = my_decorator(function_to_be_mocked_undecorated)
      decorated_funtion()
  </code></pre>
  
  <p>As you can see, I have several versions of the original function function_to_be_mocked, one with the decorator my_decorator and one 'naked'. The runner function run_decorated() calls the decorated version of function_to_be_mocked and run_undecorated() calls the undecorated version and applies the decorator 'manually'. The result of both are the same:</p>
  
  <pre><code>decorated
  original
  </code></pre>
  
  <p>Now I want to test the runner function but I need to mock the original function function_to_be_mocked but also the mocked version should be decorated:</p>
  
  <pre><code>import unittest
  import mydecorator
  from mock import patch
  
  def mock_function():
      print 'mockified'
  
  class Test(unittest.TestCase):
  
      @patch('mydecorator.function_to_be_mocked_undecorated')
      def test_undecorated_mocked(self, mock_function_to_be_mocked_undecorated):
          mydecorator.function_to_be_mocked_undecorated = mock_function
          mydecorator.run_undecorated()
          assert 1==0
  
      @patch('mydecorator.function_to_be_mocked')
      def test_decoratorated_mocked(self, mock_function_to_be_mocked):
          mydecorator.function_to_be_mocked = mock_function
          mydecorator.run_decorated()
          assert 1==0
  </code></pre>
  
  <p>This works as expected for the undecorated version test_undecorated_mocked:</p>
  
  <pre><code>decorated
  mockified
  </code></pre>
  
  <p>But the decorated version gives:</p>
  
  <pre><code>mockified
  </code></pre>
  
  <p>so the decorator vanished. </p>
  
  <p>Is it possible to get the decorated version working in the same way as the undecorated version, where the decorator is applied 'manually'?</p>
  
  <p>I tried to expose the inner function in the decorator without success.</p>
  
  <p>I saw this question <a href="https://stackoverflow.com/questions/10302162/how-do-you-mock-a-function-which-has-decorator-apply-to-it-in-a-unit-test">How do you mock a function which has decorator apply to it in a unit test?</a> but this doesn't help me.</p>