0

我有一个触发两个发布请求的功能,例如:

def send_posts():
    first_response = requests.post(
            url="google.com",
            json={'records': [{'value': message}]},
        )
    second_response = requests.post(
            url="apple.com",
            json={'records': [{'value': message}]},
        )

在我的单元测试中,我有这样的东西:

@patch('requests.post')
def send_e2e_kafka_with_env_vars(mock_post, expect, monkeypatch):
    send_posts()

    args, kwargs = mock_post.call_args // returns kwargs for 2nd post

    # I've tried, but get ' ValueError: too many values to unpack (expected 2)'
    my_first_call = mock_post.mock_calls[0]
    args, kwargs = my_first_call[0]

最终,我希望断言第一篇文章的 URL 是“google.com”。我怎样才能做到这一点?

4

1 回答 1

1

每个模拟调用都有 3 个参数,所以如果你试图解包它们,你需要匹配它(你可能想忽略第一个,因此是_):

@patch('requests.post')
def send_e2e_kafka_with_env_vars(mock_post, expect, monkeypatch):
    send_posts()

    args, kwargs = mock_post.call_args
    _, args, kwargs = mock_post.mock_calls[0]
于 2019-10-30T16:05:55.953 回答