Skip to content

Mocking coroutine called on handler #59

@lecardozo

Description

@lecardozo

I'm currently trying to mock a coroutine that is called inside the .get method of a handler. Let's call it, dummy_function.

from tornado.web import RequestHandler
from dummy import dummy_function

def Handler(RequestHandler):
    async def get(self):
        await dummy_function()

My initial approach for mocking this function was to apply the mock on the app fixture

from server.application import MyApplication

async def mocked_dummy():
    return []

@pytest.fixture
@patch("path.to.handler.dummy_function", mocked_dummy)
def app():
    return MyApplication()

@pytest.mark.gen_test
async def test_api(http_client, base_url):
   response = await http_client.fetch(base_url + "/endpoint")
   # run asserts...

When I run this, the patch doesn't work and the original dummy_function is not replaced. Is this the right approach?

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions