title: Mock instance created from class stored in variable
body: <p>I have the following code:</p>
  
  <pre class="lang-py prettyprint-override"><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()
  </code></pre>
  
  <p>I want to assert that the <code>Message.publish()</code> method is called. How do I achieve this?</p>
  
  <p>I've already tried the following ways:</p>
  
  <ol>
  <li>Assign <code>message_class</code> to <code>Mock</code> or <code>Mock()</code>. If I debug what <code>message_class(body)</code> 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).</li>
  <li>Patch <code>Message</code> class with decorator. Whenever I do this it seems like it does not catch it. When I debug what <code>message_class(body)</code> returns its of <code>Message</code> type, not Mock.</li>
  <li>Try to mock the <code>__init__</code> method of <code>message_class</code> in 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 <code>__init__</code> method is not suppose to have a return value.</li>
  </ol>