Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add assert_any_call_with #242

Open
kkpattern opened this issue Jun 29, 2023 · 0 comments
Open

Add assert_any_call_with #242

kkpattern opened this issue Jun 29, 2023 · 0 comments

Comments

@kkpattern
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant