title: Patch a class method used in a view
body: <p>I'm trying to test my view given certain responses from AWS. To do this I want to patch a class I wrote to return certain things while running tests.</p>
  <pre><code> @patch.object(CognitoInterface, &quot;get_user_tokens&quot;, return_value=mocked_get_user_tokens_return)
  
  
  class TestLogin(TestCase):
  
      def test_login(self, mocked_get_user_tokens):
          print(CognitoInterface().get_user_tokens(&quot;blah&quot;, &quot;blah&quot;)) # Works, it prints the patched return value
  
          login_data = {&quot;email&quot;: &quot;whatever@example.com&quot;, &quot;password&quot;: &quot;password&quot;}
          response = self.client.post(reverse(&quot;my_app:login&quot;), data=login_data)
  </code></pre>
  <p>Inside the view from &quot;my_app:login&quot;, I call...</p>
  <pre><code>CognitoInterface().get_user_tokens(email, password)
  </code></pre>
  <p>But this time, it uses the real method. I want it to use the patched return here as well.</p>
  <p>It seems my patch only applies inside the test file. How can I make my patch apply to all code during the test?</p>
  <p>Edit: Never figured out why @patch.object wasn't working. I just used @patch(&quot;path.to.file.from.project.root.ClassName.method_to_patch&quot;).</p>
  <p>Also, see: <a href="http://bhfsteve.blogspot.com/2012/06/patching-tip-using-mocks-in-python-unit.html" rel="nofollow noreferrer">http://bhfsteve.blogspot.com/2012/06/patching-tip-using-mocks-in-python-unit.html</a></p>