timsavage/asyncmock

How can I use AsyncMock with Factory Boy?

Closed this issue · 2 comments

I'm trying to achvieve things described here:
FactoryBoy/factory_boy#702

I tried to change model to AsyncMock and set attributes like in normal python object but when I do chat = await event.get_chat() and then print(chat.id) I get - <AsyncMock name='group_create.event.get_chat().id' id='139628156235504'>.

I understand that func.event.get_chat(return_value=self.chat) is not enough and I must set callable return value other way?

class TestGroupCreate(asynctest.TestCase):
    """Test group create."""

    async def setUp(self):
        """Prepare test for group_create."""
        self.message = TelethonMessageFactory()
        self.bot = TelethonEntityFactory()
        self.sender = TelethonEntityFactory()
        self.chat = TelethonChatFactory()

    @patch('bot.main.group_create')
    async def test_group_create(self, func):
        """Test group_create."""
        func.event.message(return_value=self.message)
        func.event.get_chat(return_value=self.chat)
        func.event.get_sender(return_value=self.sender)
        func.event.get_entity(return_value=self.bot)
        await group_create.__wrapped__(func.event)
        assert 1 == 2

Chat is:

class TelethonChatFactory(ObjectFactory):
    """Factory for Telethon Chat."""

    id = factory.Sequence(lambda n: n)
    title = factory.fuzzy.FuzzyText()

This is probably better suited to Stack Overflow and requires a lot more context not related to this library.

This library just adds support for awaiting on a mock callable, your issue looks to be more about using mock objects.