我如何测试ValueError返回的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这样做,因为我比较两个对象(错误)我如何在pytest中做到这一点?

将豁免对象与相等操作符进行比较将返回False。有几种方法可以做到这一点,例如:

e = response['body']
e2 = ValueError('Missing param: name')
assert(type(e) is type(e2) and e.args == e2.args)

这是一件很奇怪的事情。

查看更多信息:比较Python中的异常对象

相关内容

最新更新