Я пытаюсь использовать unittest.mock
, но получаю сообщение об ошибке:
AttributeError: не имеет атрибута 'get_pledge_frequency'
У меня естьследующая структура файла:
pledges/views/
├── __init__.py
├── util.py
└── user_profile.py
pledges/tests/unit/profile
├── __init__.py
└── test_user.py
Внутри pledges/views/__init___.py
У меня есть:
from .views import *
from .account import account
from .splash import splash
from .preferences import preferences
from .user_profile import user_profile
Внутри user_profile.py
У меня есть функция с именем user_profile
, которая вызывает функцию внутри util.py
называется get_pledge_frequency
следующим образом:
def user_profile(request, user_id):
# some logic
# !!!!!!!!!!!!!!!!
a, b = get_pledge_frequency(parameter) # this is the function I want to mock
# more logic
return some_value
У меня есть тест внутри test_user.py
следующим образом:
def test_name():
with mock.patch(
"pledges.views.user_profile.get_pledge_frequency"
) as get_pledge_frequency:
get_pledge_frequency.return_value = ([], [])
response = c.get(
reverse("pledges:user_profile", kwargs={"user_id": user.id})
) # this calls the function user_profile inside pledges.user_profile
# some asserts to verify functionality
Я проверил другие вопросы, но ответы не охватывают, когда естьявляется ли функция модулем, и она импортируется в файл __init__
.
Итак, есть ли способ решить эту проблему?Я в основном переименовал файл user_profile.py
в profile
, а затем я изменил тесты для ссылки на функцию внутри этого модуля, но мне интересно, возможно ли сохранить функцию и модуль с тем же именем.