aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Sebastiaan Zeeff <[email protected]>2019-11-15 01:28:56 +0100
committerGravatar Sebastiaan Zeeff <[email protected]>2019-11-15 01:28:56 +0100
commit2c77288eb3ff081e70508094bb8d030900860259 (patch)
tree733f6b6fabf38f0bdb10ecff62ba1cdad3e1f179
parentAdd `return_value` support and assertions to AsyncIteratorMock (diff)
Add MockUser to mock `discord.User` objects
I have added a special mock that follows the specifications of a `discord.User` instance. This is useful, since `Users` have less attributes available than `discord.Members`. Since this difference in availability of information can be important, we should not use a `MockMember` to mock a `discord.user`.
-rw-r--r--tests/helpers.py19
1 files changed, 19 insertions, 0 deletions
diff --git a/tests/helpers.py b/tests/helpers.py
index 50652ef9a..4da6bf84d 100644
--- a/tests/helpers.py
+++ b/tests/helpers.py
@@ -303,6 +303,25 @@ class MockMember(CustomMockMixin, unittest.mock.Mock, ColourMixin, HashableMixin
self.mention = f"@{self.name}"
+# Create a User instance to get a realistic Mock of `discord.User`
+user_instance = discord.User(data=unittest.mock.MagicMock(), state=unittest.mock.MagicMock())
+
+
+class MockUser(CustomMockMixin, unittest.mock.Mock, ColourMixin, HashableMixin):
+ """
+ A Mock subclass to mock User objects.
+
+ Instances of this class will follow the specifications of `discord.User` instances. For more
+ information, see the `MockGuild` docstring.
+ """
+ def __init__(self, **kwargs) -> None:
+ default_kwargs = {'name': 'user', 'id': next(self.discord_id), 'bot': False}
+ super().__init__(spec_set=user_instance, **collections.ChainMap(kwargs, default_kwargs))
+
+ if 'mention' not in kwargs:
+ self.mention = f"@{self.name}"
+
+
# 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