[Python] Getting TypeError: 'coroutine' object does not support the asynchronous context...

Discussão em 'Python' iniciado por Stack, Janeiro 27, 2025.

  1. Stack

    Stack Membro Participativo

    I am writing test for upload_payment_slip_file method and getting given above in the title. I saw the related issue but fortunately it did not help me. Seems like mocking enter/exit methods does not make sense. Am I missing something?

    SapHttpClient = NewType("HttpClient", httpx.AsyncClient)

    @pytest.fixture
    async def response() -> AsyncMock:
    class TestMockResponse:
    file: ClassVar[bytes] = b'test'
    response_headers: ClassVar[dict[str, Any]] = {"content-type": "application/pdf"}

    response = MagicMock()
    response.aread.return_value = AsyncMock(return_value=TestMockResponse.file)
    response.headers.return_value = MagicMock(return_value=TestMockResponse.response_headers)
    yield response
    response.reset_mock()


    @pytest.fixture
    async def mock_stream_response(
    response: AsyncMock,
    ):
    class TestMockResponse:
    file: ClassVar[bytes] = b'test'
    headers: ClassVar[dict[str, str]] = {"content-type": "application/pdf"}

    mock = AsyncMock()
    mock.raise_for_status.return_value = MagicMock(status=200)
    mock.__aenter__ = lambda _: response
    mock.__aexit__ = lambda _: None
    mock.aread.return_value = TestMockResponse.file

    yield mock
    mock.reset_mock()

    async def test_get_payment_slip_pdf(
    mock_stream_response: AsyncMock,
    injection_context: aioinject.InjectionContext,
    ) -> None:
    client = await injection_context.resolve(PaymentSapClient)
    with patch.object(
    client._client,
    client._client.stream.__name__,
    mock_stream_response
    ):
    result = await client.upload_payment_slip_file(input_=MagicMock())
    assert result.response_headers == {"content-type": "application/pdf"}

    Continue reading...

Compartilhe esta Página