cdm
cdm

Reputation: 799

Access multiple calls in mock_post

I have a function that fires off two post requests, e.g:

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}]},
        )

In my unittest, I have something like this:

@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]

Ultimately, I'm looking to assert that the URL of the first post is 'google.com'. How can I do that?

Upvotes: 0

Views: 129

Answers (1)

tmt
tmt

Reputation: 8654

Each mock call has 3 arguments so if you are trying to unpack them, you need to match it (and you probably want to ignore the first one, hence the _):

@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]

Upvotes: 1

Related Questions