我有一个装饰器需要类似这样的测试:
# code.py
from flask import request, current_app
def some_decorator(f):
@wraps(f)
def decorated_function(*args, **kwargs):
if current_app.config['ENVIRONMENT'] == 'test':
pass
if request.method == 'POST':
pass
return f(*args, **kwargs)
return decorated_function
带测试:
import mock
@mock.patch('flask.request')
def test_some_decorator(mock_req):
mock_req.method = 'POST'
function = lambda x: x
decorated_function = some_decorator(function)
function(1)
现在的问题是,模拟request.method
值不会被'POST'
模拟值覆盖,而是最终得到'GET'
我认为是默认值的值。
有什么我做错了吗?