[英]How to handle abort() while unit-testing Google Cloud Functions using Mock
我正在測試的雲函數調用abort(410)
,我想測試我的單元測試中是否收到了正確的HTTP錯誤代碼。
在簡單地發出請求時,我收到以下錯誤:
/usr/lib/python3.7/site-packages/werkzeug/exceptions.py:707: in abort
return _aborter(status, *args, **kwargs)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
self = <werkzeug.exceptions.Aborter object at 0x7fd474096b00>, code = 410, args = (), kwargs = {}
def __call__(self, code, *args, **kwargs):
if not args and not kwargs and not isinstance(code, integer_types):
raise HTTPException(response=code)
if code not in self.mapping:
raise LookupError('no exception for %r' % code)
> raise self.mapping[code](*args, **kwargs)
E werkzeug.exceptions.Gone: 410 Gone: The requested URL is no longer available on this server and there is no forwarding address. If you followed a link from a foreign page, please contact the author of this page.
/usr/lib/python3.7/site-packages/werkzeug/exceptions.py:687: Gone
這是我制作Mock請求的代碼:
from unittest.mock import Mock
from cloud_functions import main
data = { ... }
headers = { ... }
req = Mock(get_json=Mock(return_value=data), args=data, headers=headers)
resp = main.my_function(req)
使用werkzeug.exceptions
的HTTPException
模塊來捕獲錯誤代碼對我來說很好。
將我的代碼更改為此使其現在可以正常工作。
from unittest.mock import Mock
from cloud_functions import main
from werkzeug.exceptions import HTTPException
data = { ... }
headers = { ... }
req = Mock(get_json=Mock(return_value=data), args=data, headers=headers)
try:
resp = main.get_url_full(req)
assert False
except HTTPException as e:
assert e.code == 410
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.