I have the following code:
class Messenger(object):
def __init__(self):
# Class Type of what messages will be created as.
message_class = Message
def publish(self, body):
# Instantiate object of type stored in `message_class`
message = message_class(body)
message.publish()
I want to assert that the Message.publish() method is called. How do I achieve this?
I've already tried the following ways:
- Assign
message_classtoMockorMock(). If I debug whatmessage_class(body)returns, it is a Mock, but I don't seem to be able to get the instance and assert it (because the Mock I assign in my test is not the instance used, it is the Type). - Patch
Messageclass with decorator. Whenever I do this it seems like it does not catch it. When I debug whatmessage_class(body)returns its ofMessagetype, not Mock. - Try to mock the
__init__method ofmessage_classin hopes that I can set the instance that is returned whenever the code tries to Instantiate the message. Does not work, throws errors because the__init__method is not suppose to have a return value.