title: How to patch a method decorated with `flask - route` in testing?
body: <p>I have a python application which uses Flask to expose some endpoints. Also, I'm using a <code>fixture</code>, to catch unhandled exceptions and to return a custom response. This is a sample code:</p>
  <pre><code>from flask import make_response, Blueprint
  
  root = Blueprint(&quot;main&quot;, __name__)
  
  @root.errorhandler(Exception)
  def custom_error_handler(error):
      #do other things here
      return make_response({&quot;status&quot;: &quot;failure&quot;, &quot;error&quot;: str(error)}), 500
  
  @root.route(&quot;/my_url&quot;, methods=[&quot;POST&quot;])
  def my_url_method():
      #do other thins
      return make_response(...), 200
  </code></pre>
  <p>I want to have a test to ensure this works. So, in order to simulate that an unhandled exception has occurred, I'm trying to mock <code>my_url method</code> with a function that simply raise an exception:</p>
  <pre><code>from unittest.mock import patch
  from flask import Flask
  
  @pytest.fixture
  def client(monkeypatch):
      app = Flask(__name__, instance_relative_config=True)
      app.register_blueprint(root)
      app.config[&quot;TESTING&quot;] = True
  
      return app.test_client()
  
  def test_exception(client):
      with patch(&quot;[file_link].my_url_method&quot;, side_effect=Exception(&quot;an error&quot;)):
          response = client.post(&quot;my_url&quot;)
          assert response.status_code == 500
  </code></pre>
  <p>However assertion fails. The method is executed correctly as is not raised any exception, returning 200 as status code.</p>
  <p>I think the problem is that mock is not applied when you call the method throw flask. But I don't know how to fix it.</p>