pnuckowski / aioresponses

Aioresponses is a helper for mock/fake web requests in python aiohttp package.
MIT License
513 stars 86 forks source link

Add assert_any_call_with #242

Open kkpattern opened 1 year ago

kkpattern commented 1 year ago

Currently, assert_any_call doesn't check the request body, only the params. Sometimes we want to check the request body. We cannot use assert_called_with because the request we want to check is not the last one. We tried an implementation locally, and it works:

    def assert_any_call_with(
        self,
        url: "Union[URL, str, Pattern]",
        method: str = hdrs.METH_GET,
        *args: Any,
        **kwargs: Any
    ):
        """assert the mock has been called with the specified arguments.
        The assert passes if the mock has *ever* been called, unlike
        `assert_called_with` and `assert_called_once_with` that only pass if
        the call is the most recent one."""
        url = normalize_url(merge_params(url, kwargs.get("params")))
        method = method.upper()
        key = (method, url)

        try:
            request_list = self.requests[key]
        except KeyError:
            expected_string = self._format_call_signature(
                url, method=method, *args, **kwargs
            )
            raise AssertionError("%s call not found" % expected_string)
        else:
            expected = self._build_request_call(method, *args, **kwargs)
            found = False
            for actual in request_list:
                if expected == actual:
                    found = True
                    break
            if not found:
                expected_string = self._format_call_signature(
                    url, method=method, *args, **kwargs
                )
                raise AssertionError("%s call not found" % expected_string)

If assert_any_call_with is useful, we'd love to create a PR.