我如何测试正文中返回的 ValueError
How can i test for ValueError returned in body
我有测试:
# Should return 400 if no name is provided
def test_no_name(self):
sut = SignUpController()
http_request = {
"email": "any_email",
"password": "any_password",
"password_confirmation": "any_password"
}
response = sut.handle(http_request)
assert response['statusCode'] == 400
assert response['body'] == ValueError('Missing param: name')
我有生产代码:
class SignUpController:
def handle(self, http_request: any) -> any:
return {
"statusCode": 400,
"body": ValueError('Missing param: name')
}
它告诉我值不相等
E AssertionError: assert ValueError('Missing param: name') == ValueError('Missing param: name')
E + where ValueError('Missing param: name') = ValueError('Missing param: name')
我猜“断言”是错误的..
在 Javascript 的 Jest 中,我用 toEqual 来做这个,因为我正在比较 2 个对象(错误)
我如何在 pytest 中做到这一点?
用相等运算符比较豁免对象会return错误。有几种方法可以做到,例如:
e = response['body']
e2 = ValueError('Missing param: name')
assert(type(e) is type(e2) and e.args == e2.args)
虽然这很奇怪。
查看此处了解更多信息:
Comparing Exception Objects in Python
我有测试:
# Should return 400 if no name is provided
def test_no_name(self):
sut = SignUpController()
http_request = {
"email": "any_email",
"password": "any_password",
"password_confirmation": "any_password"
}
response = sut.handle(http_request)
assert response['statusCode'] == 400
assert response['body'] == ValueError('Missing param: name')
我有生产代码:
class SignUpController:
def handle(self, http_request: any) -> any:
return {
"statusCode": 400,
"body": ValueError('Missing param: name')
}
它告诉我值不相等
E AssertionError: assert ValueError('Missing param: name') == ValueError('Missing param: name')
E + where ValueError('Missing param: name') = ValueError('Missing param: name')
我猜“断言”是错误的.. 在 Javascript 的 Jest 中,我用 toEqual 来做这个,因为我正在比较 2 个对象(错误) 我如何在 pytest 中做到这一点?
用相等运算符比较豁免对象会return错误。有几种方法可以做到,例如:
e = response['body']
e2 = ValueError('Missing param: name')
assert(type(e) is type(e2) and e.args == e2.args)
虽然这很奇怪。
查看此处了解更多信息: Comparing Exception Objects in Python