我正在写一个单元测试使用CakePHP的一个方法当作对象TimeHelperCakePHP的TimeHelper时间戳通过PHPUnit的
public function testData()
{
$timestamp = (string)Time::now()->i18nFormat('YYYY-MM-dd HH:mm:ss');
// dump($timestamp);
// "2015-10-14 23:25:33"
...
# data request should update the last_checkin field
$screen = TableRegistry::get('Screens')->get(1);
$this->assertEquals($timestamp, $screen->last_checkin);
}
我真的很困惑,为什么它的工作原理在控制器,并从dump()
返回是正确的。但是,单元测试失败以下消息:
1) App\Test\TestCase\Controller\ScreensControllerTest::testData
Failed asserting that Cake\I18n\Time Object &000000001a943b50000000016cb13674 (
'date' => '2015-10-14 23:25:33.000000'
'timezone_type' => 3
'timezone' => 'UTC'
) matches expected '2015-10-14 23:25:33'.
/mysite/tests/TestCase/Controller/ScreensControllerTest.php:103
为什么assertEquals()
认为这是一个对象时dump()
(和save()
在我的控制器),认为它是一个字符串?
这是一个TimeHelper错误?一个phpunit错误?或者我错过了什么?
非常感谢您雄辩的回答。我现在明白了这个问题,但我仍然困惑为什么将TimeHelper对象作为字符串投射并不会触发魔术__toString()。 PHP文档不讨论这一点。 – emersonthis
好的。所以还是有点神秘。使用'$ timestamp - > __ toString()'可以让我的测试通过本地。但在circleCI中失败:'PHP致命错误:调用string_上的成员函数__toString() – emersonthis