如何使用 python pytest 模拟实例化模拟对象的响应
我正在尝试模拟一个调用 sendgrid API 的函数。我想模拟 API 库,但不知道哪里出了问题。
调用 API 的函数:
def mailing_list_signup(data: dict):
email_address = data["email"]
name = data["contact_name"]
API_KEY = settings.SENDGRID_API_KEY
sg = SendGridAPIClient(API_KEY)
# https://docs.sendgrid.com/api-reference/contacts/add-or-update-a-contact
data = {
"contacts": [
{
"email": email_address,
"name": name,
}
]
}
response = sg.client.marketing.contacts.put(request_body=data)
return response
我的错误测试:
@dataclass
class APIResponse:
status_code: int = 202
body: bytes = b"example"
@override_settings(SENDGRID_API_KEY='123')
def test_mailing_list_signup():
response = APIResponse()
with mock.patch("myapp.apps.base.business.SendGridAPIClient") as sendgridAPI:
sendgridAPI.client.marketing.contacts.put.return_value = response
data = {
"email": "[email protected]",
"contact_name": None,
}
result = mailing_list_signup(data)
assert result == response
Pytest 告诉我测试失败,并显示以下消息:
FAILED myapp/apps/base/tests/test_business.py::test_mailing_list_signup - AssertionError: assert <MagicMock name='SendGridAPIClient().client.marketing.contacts.put()' id='4622453344'> == APIClient(status_code=202, body=b'example')
I'm trying to mock a function that calls the sendgrid API. I want to mock the API library but can't work out where I'm going wrong.
Function which calls the API:
def mailing_list_signup(data: dict):
email_address = data["email"]
name = data["contact_name"]
API_KEY = settings.SENDGRID_API_KEY
sg = SendGridAPIClient(API_KEY)
# https://docs.sendgrid.com/api-reference/contacts/add-or-update-a-contact
data = {
"contacts": [
{
"email": email_address,
"name": name,
}
]
}
response = sg.client.marketing.contacts.put(request_body=data)
return response
my bad test:
@dataclass
class APIResponse:
status_code: int = 202
body: bytes = b"example"
@override_settings(SENDGRID_API_KEY='123')
def test_mailing_list_signup():
response = APIResponse()
with mock.patch("myapp.apps.base.business.SendGridAPIClient") as sendgridAPI:
sendgridAPI.client.marketing.contacts.put.return_value = response
data = {
"email": "[email protected]",
"contact_name": None,
}
result = mailing_list_signup(data)
assert result == response
Pytest tells me the test failed with the following message:
FAILED myapp/apps/base/tests/test_business.py::test_mailing_list_signup - AssertionError: assert <MagicMock name='SendGridAPIClient().client.marketing.contacts.put()' id='4622453344'> == APIClient(status_code=202, body=b'example')
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于正在模拟可调用对象的返回值,因此应在可调用对象而不是属性上设置返回值。
改变
sendgridAPI.client.marketing.contacts.put.return_value = 响应
到
sendgridAPI.client.marketing.contacts.put().return_value = 响应
Because a return value of a callable is being mocked, the return value should be set on a callable and not an attribute.
change
sendgridAPI.client.marketing.contacts.put.return_value = response
to
sendgridAPI.client.marketing.contacts.put().return_value = response