谁能告诉我为什么这不起作用?
>>> import mock
>>> @mock.patch('datetime.date.today')
... def today(cls):
... return date(2010, 1, 1)
...
>>> from datetime import date
>>> date.today()
datetime.date(2010, 12, 19)
Run Code Online (Sandbox Code Playgroud)
也许有人可以建议一个更好的方法?
我有一个参数默认值为 的函数datetime.now()。方法如下,
def as_standard_format(p_date=datetime.now(), fmt=sdk_constants.DEFAULT_DATE_TIME_FORMAT):
return p_date.strftime(fmt)
Run Code Online (Sandbox Code Playgroud)
我有一个类似下面的测试方法,
@freeze_time(datetime(year=2018, month=9, day=25, hour=15, minute=46, second=36))
def testNowWithDefaultFormat(self):
# The below method gives the frozen time
print(datetime.now())
# But the below test fails
assert utils.format_date(
datetime.now()) == "20180925T154636Z", "Default call not giving current time in " \
"standard format \"%Y%m%dT%H%M%SZ\""
Run Code Online (Sandbox Code Playgroud)
为什么不能freeze_time使用默认参数值?