我想知道如何模拟create_bucket
函数中的verify_token
方法,从而引发异常
other_module.py
# ...
def get_connection(backend=None, **kwargs):
klass = import_string(backend or settings.BACKEND)
return klass(**kwargs)
module.py
from other_module import get_connection
def create_bucket():
conn = get_connection()
# ...
try:
conn.verify_token()
except VerificationFailed:
#...
tests.py
测试生成错误消息
AttributeError: module.create_bucket.get_connection does not have the attribute 'verify_token'
def test_create_bucket_with_failed_verification(mocker):
mocker.patch.object(
"module.get_connection",
"verify_token",
side_effect=VerificationFailed
)
我通过模拟整个
get_connection
函数解决了这个问题:相关问题 更多 >
编程相关推荐