Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在Flask中模拟函数装饰器?_Python_Unit Testing_Flask_Python Decorators_Python Mock - Fatal编程技术网

Python 如何在Flask中模拟函数装饰器?

Python 如何在Flask中模拟函数装饰器?,python,unit-testing,flask,python-decorators,python-mock,Python,Unit Testing,Flask,Python Decorators,Python Mock,我有一个名为myviews.py的文件,它位于名为views的目录下,而该目录又位于名为src的目录下,其中包含一些看起来像这样的视图: from utils import verify_refresh_token fetch_views = Blueprint('foo', __name, url_prefix='bar') @fetchviews.route('baz', methods=['GET']) @verify_refresh_token def process_request(

我有一个名为
myviews.py
的文件,它位于名为
views
的目录下,而该目录又位于名为
src
的目录下,其中包含一些看起来像这样的视图:

from utils import verify_refresh_token
fetch_views = Blueprint('foo', __name, url_prefix='bar')

@fetchviews.route('baz', methods=['GET'])
@verify_refresh_token
def process_request():
    return 'test'
utils.py
中,我有一个verify\u refresh\u token函数,它或多或少地执行以下操作:

def verify_refresh_token(func):
   @wraps(func)
   def wrapper(*args, **kwargs):
      return func(*args, **kwargs)
   return wrapper
在我的测试文件中,我有如下内容:

from src.main import app
....
def setUp(self):
   with mock.patch('src.views.myviews.verify_refresh_token') as mock_verify_token:
      self.test_app = app.test_client(self)

def test_stuff(self):
   self.tester.post('bar/baz', data={})
无论我修补什么(将
src.views.myviews.verify\u refresh\u token
更改为
src.utils.verify\u refresh\u token
等),装饰程序仍然会被调用并崩溃我的应用程序。模仿这个装饰师的正确方法是什么?视图被添加到
main.py
中,如下所示:

app = Flask(__name__)
from views.myviews import fetchviews
app.register_blueprint(fetchviews)

正确的方法是模拟decorator内部的行为,因为decorator是在定义函数时调用的,而不是在执行函数时调用的。我假设在
verify\u refresh\u token
中有一个东西可以验证某个令牌,而您的帖子中没有包含该令牌。取而代之的是嘲弄它。@univerio,就是它耍了这个把戏!我不得不换个模子。谢谢@ClicqueottheDog,你为什么不发布你的解决方案?