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

Mocking coroutine called on handler #59

Open
lecardozo opened this issue Dec 4, 2019 · 1 comment
Open

Mocking coroutine called on handler #59

lecardozo opened this issue Dec 4, 2019 · 1 comment

Comments

@lecardozo
Copy link

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!

@dingyaguang117
Copy link

Please try

@pytest.mark.gen_test
async def test_api(http_client, base_url):
   with patch("path.to.handler.dummy_function", mocked_dummy):
     response = await http_client.fetch(base_url + "/endpoint")
   # run asserts...

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

2 participants