aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorGravatar Sebastiaan Zeeff <[email protected]>2019-11-14 10:58:40 +0100
committerGravatar Sebastiaan Zeeff <[email protected]>2019-11-14 10:58:40 +0100
commit61051f9cc5abbf571dfa13c49324109ef16f78fc (patch)
tree33eefa7c66ddc698390ee51b6fbe32244a99befa /tests
parentAdd bot=False default value to MockMember (diff)
Add MockAttachment type and attachments default for MockMessage
As stated from the start, our intention is to add custom mock types as we need them for testing. While writing tests for DuckPond, I noticed that we did not have a mock type for Attachments, so I added one with this commit. In addition, I think it's a very sensible for MockMessage to have an empty list as a default value for the `attachements` attribute. This is equal to what `discord.Message` returns for a message without attachments and makes sure that if you don't explicitely add an attachment to a message, `MockMessage.attachments` tests as falsey.
Diffstat (limited to 'tests')
-rw-r--r--tests/helpers.py17
1 files changed, 16 insertions, 1 deletions
diff --git a/tests/helpers.py b/tests/helpers.py
index 199d45700..3e43679fe 100644
--- a/tests/helpers.py
+++ b/tests/helpers.py
@@ -355,6 +355,20 @@ class MockContext(CustomMockMixin, unittest.mock.MagicMock):
self.channel = kwargs.get('channel', MockTextChannel())
+attachment_instance = discord.Attachment(data=unittest.mock.MagicMock(id=1), state=unittest.mock.MagicMock())
+
+
+class MockAttachment(CustomMockMixin, unittest.mock.MagicMock):
+ """
+ A MagicMock subclass to mock Attachment objects.
+
+ Instances of this class will follow the specifications of `discord.Attachment` instances. For
+ more information, see the `MockGuild` docstring.
+ """
+ def __init__(self, **kwargs) -> None:
+ super().__init__(spec_set=attachment_instance, **kwargs)
+
+
class MockMessage(CustomMockMixin, unittest.mock.MagicMock):
"""
A MagicMock subclass to mock Message objects.
@@ -364,7 +378,8 @@ class MockMessage(CustomMockMixin, unittest.mock.MagicMock):
"""
def __init__(self, **kwargs) -> None:
- super().__init__(spec_set=message_instance, **kwargs)
+ default_kwargs = {'attachments': []}
+ super().__init__(spec_set=message_instance, **collections.ChainMap(kwargs, default_kwargs))
self.author = kwargs.get('author', MockMember())
self.channel = kwargs.get('channel', MockTextChannel())