Skip to content

Does doublex work with async calls? #37

Description

@dimanu-py

When using doublex with async calls the behavior is not as expected. I have the following test:

@pytest.mark.asyncio
async def test_should_register_a_valid_user(self) -> None:
    user = UserMother.random()
    repository = Mock(UserRepository)
    expect_call(repository).save(user) 
    user_registrar = UserRegistrar(repository=repository)
    command = RegisterUserCommand(
        id=user._id,
        name=user._name,
        username=user._username,
        email=user._email,
        profile_picture=user._profile_picture,
    )

    await user_registrar(command)

    expect(repository).to(have_been_satisfied)

This exact same test without the await (and therefore the function not being async) passes correctly. But setting it as async makes the fail test: TypeError: object NoneType can't be used in 'await' expression

My guess is that this is happening because the async function awaits something "awaitable" but the mock is returning None instead of a coroutine. For now I have get the test to pass with this:

@staticmethod
def _immediate_future(result = None) -> asyncio.Future:
    future = asyncio.Future()
    future.set_result(result)
    return future

@pytest.mark.asyncio
async def test_should_register_a_valid_user(self) -> None:
    ...
    expect_call(repository).save(user) .returns(self._immediate_future())

However it would be nice to have this kind of functionality out of the box, as for now, I cannot use a Spy as I was using in a previous version

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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