title: How to mock python's datetime.now() in a class method for unit testing?
body: <p>I'm trying to write tests for a class that has methods like:</p>
  
  <pre><code>import datetime
  import pytz
  
  class MyClass:
      def get_now(self, timezone):
          return datetime.datetime.now(timezone)
  
      def do_many_things(self, tz_string='Europe/London'):
          tz = pytz.timezone(tz_string)
          localtime_now = self.get_now(tz)
          ...
          return things
  </code></pre>
  
  <p>I want to test it, and to do so I need to make sure that the <code>datetime.datetime.now()</code> call returns something predictable.</p>
  
  <p>I've been reading lots of examples of using <a href="http://www.voidspace.org.uk/python/mock/" rel="noreferrer">Mock</a> in tests, but haven't found anything quite like what I need, and I can't work out how to use it in my tests.</p>
  
  <p>I separated the <code>get_now()</code> method out in case it's easier to mock that, instead of <code>datetime.datetime.now()</code>, but I'm still stumped. Any thoughts on how to write UnitTests for this using Mock? (This is all in Django, fwiw; I'm not sure if this makes a difference in this case.)</p>