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

Why MagicMock can't be used in 'await' expression? #140

Open
heckad opened this issue Aug 29, 2019 · 2 comments
Open

Why MagicMock can't be used in 'await' expression? #140

heckad opened this issue Aug 29, 2019 · 2 comments

Comments

@heckad
Copy link

heckad commented Aug 29, 2019

For example, I have the same code

import asynctest


class SomeAsyncClass:
    async def __aenter__(self):
        pass

    async def __aexit__(self, exc_type, exc_val, exc_tb):
        pass

    async def do_something(self):
        pass


async def method_using_SomeAsyncClass():
    async with SomeAsyncClass() as s:
        return await s.do_something()


@asynctest.patch('test.SomeAsyncClass')
async def test_some_test(some_async_class):
    res = await method_using_SomeAsyncClass()

And it fails with error: TypeError: object MagicMock can't be used in 'await' expression. Why and how to fix it?

@Martiusweb
Copy link
Owner

Martiusweb commented Aug 29, 2019 via email

@heckad
Copy link
Author

heckad commented Aug 29, 2019

It's not work some_async_class.__aenter__.return_value = some_async_class() The rison of this that some_async_class has been pathed. Also this

@asynctest.patch('test.SomeAsyncClass')
async def test_some_test(some_async_class):
    res = await some_async_class.do_something()

doesn't work. Why MagicMock not pathced methods in SomeAsyncClass?

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