From bd5980728bd7bfd5bba53369934698c43f12fa05 Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Wed, 29 Jan 2020 17:09:43 -0800 Subject: Sync tests: fix Syncer mocks not having async methods While on 3.7, the CustomMockMixin needs to be leveraged so that coroutine members are replace with AsyncMocks instead. --- tests/bot/cogs/sync/test_cog.py | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) (limited to 'tests') diff --git a/tests/bot/cogs/sync/test_cog.py b/tests/bot/cogs/sync/test_cog.py index 118782db3..ec66c795d 100644 --- a/tests/bot/cogs/sync/test_cog.py +++ b/tests/bot/cogs/sync/test_cog.py @@ -2,9 +2,21 @@ import unittest from unittest import mock from bot.cogs import sync +from bot.cogs.sync.syncers import Syncer from tests import helpers +class MockSyncer(helpers.CustomMockMixin, mock.MagicMock): + """ + A MagicMock subclass to mock Syncer objects. + + Instances of this class will follow the specifications of `bot.cogs.sync.syncers.Syncer` + instances. For more information, see the `MockGuild` docstring. + """ + def __init__(self, **kwargs) -> None: + super().__init__(spec_set=Syncer, **kwargs) + + class SyncExtensionTests(unittest.TestCase): """Tests for the sync extension.""" @@ -22,8 +34,17 @@ class SyncCogTests(unittest.TestCase): def setUp(self): self.bot = helpers.MockBot() - self.role_syncer_patcher = mock.patch("bot.cogs.sync.syncers.RoleSyncer", autospec=True) - self.user_syncer_patcher = mock.patch("bot.cogs.sync.syncers.UserSyncer", autospec=True) + # These patch the type. When the type is called, a MockSyncer instanced is returned. + # MockSyncer is needed so that our custom AsyncMock is used. + # TODO: Use autospec instead in 3.8, which will automatically use AsyncMock when needed. + self.role_syncer_patcher = mock.patch( + "bot.cogs.sync.syncers.RoleSyncer", + new=mock.MagicMock(return_value=MockSyncer()) + ) + self.user_syncer_patcher = mock.patch( + "bot.cogs.sync.syncers.UserSyncer", + new=mock.MagicMock(return_value=MockSyncer()) + ) self.RoleSyncer = self.role_syncer_patcher.start() self.UserSyncer = self.user_syncer_patcher.start() -- cgit v1.2.3