0

I have read through the docs and couldn't find anything about accessing the original/expected/unedited return_value of the foo_hello function and use it to make assertions. The best I could come up with is getting the parameters from the call_args and recalling the function. Using side_effect itself calls foo_hello, but when I check the foo.return_value, all I see is an AsyncMock object. Is there a way to get the return_value without needing to recall the function again?

Here is my example below:

from unittest.mock import patch
import pytest


async def foo_hello(x, y):
    return {f"hello {z}":f"world {y}"}


@pytest.mark.asyncio
async def test(client: AsyncClient):
    payload = {"foo": "bar", "hello": "world"}
    with patch(
        "module.foo_hello",side_effect=foo_hello
    ) as foo,
        patch(
        "module.create_pool",
        return_value=MockArqRedis(),
    ):
        # handler function calls foo_hello down stream
        response = await handler_function(
            request=payload,
            user_id=user.id,
        )
    
        foo.assert_called_once_with(
            x=some_value,
            y=some_value,
        )

        parameters = foo.call_args[1]
        foo_return: dict = await foo(**parameters)
     
     assert "some_value" in foo_return

Can the return_value of a mocked function/object ever be what we would expect if we were to run a function without the mock? Thanks

I tried setting wraps to foo_hello, which is similar to side_effect.

PirateNinjas
  • 1,908
  • 1
  • 16
  • 21
Hyperion
  • 1
  • 2
  • While there is some overlap between `wraps` and `side_effect`, they have different use cases. `wraps` is the option you want here. – chepner Mar 02 '23 at 22:32
  • @chepner do you know how to get the `return_value` of the unmocked function with `wraps`? – Hyperion Mar 02 '23 at 23:14

0 Answers0