我正在寫一個單元測試使用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