aboutsummaryrefslogtreecommitdiffstats
path: root/tests/helpers.py
diff options
context:
space:
mode:
authorGravatar MarkKoz <[email protected]>2019-12-31 12:19:44 -0800
committerGravatar MarkKoz <[email protected]>2020-02-12 10:07:46 -0800
commitd9407a56ba34f3a446f3fa583c0c4dec107913dc (patch)
tree2f571339a38ae7d7d324f794737b3c514c0538ee /tests/helpers.py
parentSync: handle API errors gracefully (diff)
Tests: add a MockAPIClient
Diffstat (limited to 'tests/helpers.py')
-rw-r--r--tests/helpers.py18
1 files changed, 18 insertions, 0 deletions
diff --git a/tests/helpers.py b/tests/helpers.py
index 5df796c23..71b80a223 100644
--- a/tests/helpers.py
+++ b/tests/helpers.py
@@ -12,6 +12,7 @@ from typing import Any, Iterable, Optional
import discord
from discord.ext.commands import Context
+from bot.api import APIClient
from bot.bot import Bot
@@ -324,6 +325,22 @@ class MockUser(CustomMockMixin, unittest.mock.Mock, ColourMixin, HashableMixin):
self.mention = f"@{self.name}"
+# Create an APIClient instance to get a realistic MagicMock of `bot.api.APIClient`
+api_client_instance = APIClient(loop=unittest.mock.MagicMock())
+
+
+class MockAPIClient(CustomMockMixin, unittest.mock.MagicMock):
+ """
+ A MagicMock subclass to mock APIClient objects.
+
+ Instances of this class will follow the specifications of `bot.api.APIClient` instances.
+ For more information, see the `MockGuild` docstring.
+ """
+
+ def __init__(self, **kwargs) -> None:
+ super().__init__(spec_set=api_client_instance, **kwargs)
+
+
# Create a Bot instance to get a realistic MagicMock of `discord.ext.commands.Bot`
bot_instance = Bot(command_prefix=unittest.mock.MagicMock())
bot_instance.http_session = None
@@ -340,6 +357,7 @@ class MockBot(CustomMockMixin, unittest.mock.MagicMock):
def __init__(self, **kwargs) -> None:
super().__init__(spec_set=bot_instance, **kwargs)
+ self.api_client = MockAPIClient()
# self.wait_for is *not* a coroutine function, but returns a coroutine nonetheless and
# and should therefore be awaited. (The documentation calls it a coroutine as well, which