是否可以在不需要调用函数的情况下引用未模拟函数的原始 return_value?

问题描述 投票:0回答:0

我已经通读了文档,但找不到任何有关访问

return_value
函数的原始/预期/未编辑的
foo_hello
并使用它来进行断言的信息。我能想到的最好的方法是从
call_args
获取参数并调用函数。使用
side_effect
本身调用
foo_hello
,但是当我检查
foo.return_value
时,我看到的只是一个
AsyncMock
对象。有没有办法在不需要再次调用函数的情况下得到的return_value?

下面是我的例子:

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

如果我们在没有模拟的情况下运行函数,模拟函数/对象的 return_value 是否可以成为我们期望的值?谢谢

我尝试将 wraps 设置为 foo_hello,这类似于 side_effect。

python python-3.x unit-testing python-unittest python-unittest.mock
© www.soinside.com 2019 - 2024. All rights reserved.