Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unit testing CAKEPHP3:设置单元测试的当前时间_Unit Testing_Cakephp_Cakephp 3.0 - Fatal编程技术网

Unit testing 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

在CakePHP3中,我使用内置类
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');