There are a few problems.
First of all, the way you’re using
mock.patch isn’t quite right. When used as a decorator, it replaces the given function/class (in this case,
datetime.date.today) with a
Mock object only within the decorated function. So, only within your
datetime.date.today be a different function, which doesn’t appear to be what you want.
What you really want seems to be more like this:
datetime.date.today.return_value = date(2010, 1, 1)
Unfortunately, this won’t work:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "build/bdist.macosx-10.6-universal/egg/mock.py", line 557, in patched
File "build/bdist.macosx-10.6-universal/egg/mock.py", line 620, in __enter__
TypeError: can't set attributes of built-in/extension type 'datetime.date'
This fails because Python built-in types are immutable – see this answer for more details.
In this case, I would subclass datetime.date myself and create the right function:
return cls(2010, 1, 1)
datetime.date = NewDate
And now you could do:
NewDate(2010, 1, 1)