Can anyone tell me why this isn’t working?
JavaScript
x
9
1
>>> import mock
2
>>> @mock.patch('datetime.date.today')
3
def today(cls):
4
return date(2010, 1, 1)
5
6
>>> from datetime import date
7
>>> date.today()
8
datetime.date(2010, 12, 19)
9
Perhaps someone could suggest a better way?
Advertisement
Answer
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 today()
will 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:
JavaScript
1
5
1
@mock.patch('datetime.date.today')
2
def test():
3
datetime.date.today.return_value = date(2010, 1, 1)
4
print datetime.date.today()
5
Unfortunately, this won’t work:
JavaScript
1
7
1
>>> test()
2
Traceback (most recent call last):
3
File "<stdin>", line 1, in <module>
4
File "build/bdist.macosx-10.6-universal/egg/mock.py", line 557, in patched
5
File "build/bdist.macosx-10.6-universal/egg/mock.py", line 620, in __enter__
6
TypeError: can't set attributes of built-in/extension type 'datetime.date'
7
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:
JavaScript
1
7
1
import datetime
2
class NewDate(datetime.date):
3
@classmethod
4
def today(cls):
5
return cls(2010, 1, 1)
6
datetime.date = NewDate
7
And now you could do:
JavaScript
1
3
1
>>> datetime.date.today()
2
NewDate(2010, 1, 1)
3