Unit testing CAKEPHP3:设置单元测试的当前时间
在CakePHP3中,我使用内置类Unit testing CAKEPHP3:设置单元测试的当前时间,unit-testing,cakephp,cakephp-3.0,Unit Testing,Cakephp,Cakephp 3.0,在CakePHP3中,我使用内置类Cake\I18n\Time计算API响应与当前时间的时间差。结果被存储在单元测试中,但我不知道如何设置当前时间,因此预期结果不断变化 下面是我要测试的单元: $apiTime = Time::createFromTimestamp($apiTimestamp); // Get the time the prediction was generated $currentTime = Time::now(); return (int)(($apiTime-&g
Cake\I18n\Time
计算API响应与当前时间的时间差。结果被存储在单元测试中,但我不知道如何设置当前时间,因此预期结果不断变化
下面是我要测试的单元:
$apiTime = Time::createFromTimestamp($apiTimestamp);
// Get the time the prediction was generated
$currentTime = Time::now();
return (int)(($apiTime->getTimestamp() - $currentTime->getTimestamp()) / 60);
那么,有什么方法可以设置当前时间吗?这样我就能真正知道预期的结果是什么
在测试用例中,您可以使用setTestNow()
模拟now()
:
这是碳的一个特性,Cake的
时间类基于此。Cake的时间类现在基于Chronos。资料来源:
// Fixate time.
$now = new Time('2014-04-12 12:22:30');
Time::setTestNow($now);
// Returns '2014-04-12 12:22:30'
$now = Time::now();
// Returns '2014-04-12 12:22:30'
$now = Time::parse('now');