title: How to mock view decorator in Django
body: <p>Consider that I have a simple APIView as below,</p>
  <pre class="lang-py prettyprint-override"><code>from rest_framework.views import APIView
  from rest_framework.response import Response
  
  
  def my_custom_decorator(func):
      def wrap(view, request):
          if request.method.lower():
              raise ValueError(&quot;Just for testing&quot;)
          return func(view, request)
  
      return wrap
  
  
  class SomeAPIView(APIView):
  
      @my_custom_decorator
      def post(self, request):
          return Response({&quot;message&quot;: &quot;Success&quot;})
  </code></pre>
  <p>Note that the view function <code>post(...)</code> is wrapped by the decorator <code>@my_custom_decorator</code>.
  Noe, I want to write the test for this API and I tried like this</p>
  <pre class="lang-py prettyprint-override"><code>from rest_framework.test import APITestCase
  from django.urls import reverse
  from unittest.mock import patch
  
  
  class TestSomeAPIView(APITestCase):
  
      @patch(&quot;sample.views.my_custom_decorator&quot;)
      def test_decorator(self, mock_my_custom_decorator):
          url = reverse(&quot;some-api-view&quot;)
          response = self.client.post(url)
          self.assertEqual(response.status_code, 200)
          self.assertEqual(response.json(), {&quot;message&quot;: &quot;Success&quot;})
  </code></pre>
  <p>This didn't mock the <code>@my_custom_decorator</code> properly and thus gave me an exception.</p>
  <p><strong>Question</strong>: How can I mock the <code>@my_custom_decorator</code> to retrieve a successful response?</p>
  <h3>Notes</h3>
  <ul>
  <li>I have seen this SO post, <a href="https://stackoverflow.com/questions/7667567/can-i-patch-a-python-decorator-before-it-wraps-a-function">Can I patch a Python decorator before it wraps a function?</a>, I feel that post lacks some examples,</li>
  </ul>
  <h1>Update - 1</h1>
  <p><a href="https://stackoverflow.com/a/67145715/8283848">This answer</a> will work only if the test module gets initialized before the initialization of the view module. AFAIK, this kind of loading isn't configurable in Django.</p>