如何使用 python pytest 模拟实例化模拟对象的响应

发布于 2025-01-13 21:09:25 字数 1675 浏览 0 评论 0原文

我正在尝试模拟一个调用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花间憩 2025-01-20 21:09:25

由于正在模拟可调用对象的返回值,因此应在可调用对象而不是属性上设置返回值。

改变
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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文