正在使用以下装饰器:
import flask_login as login
from functools import wraps
from myapp.utils.api_response import get_failure_response
def authenticate(f):
@wraps(f)
def wrapper(*args, **kwargs):
if login.current_user.is_anonymous:
return get_failure_response({'error': 'user not logged in'}, 400, 'please login')
return f(*args, **kwargs)
return wrapper
我正在使用pytest并编写以下测试用例:
from myapp.api.decorator import authenticate
import flask_login
from mock import mock
from mock import MagicMock
import myapp.utils.api_response as api_response
#from myapp.utils.api_response import get_failure_response, get_success_response
@mock.patch.object(flask_login, 'current_user')
@mock.patch.object(api_response, 'get_failure_response')
def test_is_authenticate_decorator(mock_current_user, mock_get_failure_response):
current_user = mock.Mock()
current_user.is_anonymous = False
mock_current_user.return_value = current_user
mock_get_failure_response.return_value = "Success"
is_authenticated = True
try:
with mock.patch('myapp.utils.api_response.get_failure_response') as MockClass:
@authenticate
def mock_func(is_authenticated):
return is_authenticated
MockClass.return_value = "Working" # Change the return value
mock_func(is_authenticated)
except Exception as e:
pass
但它始终会出现异常,并显示应用程序不存在的消息。我如何测试以下装饰器功能。