![](/img/trans.png)
[英]Failed asserting two arrays are equal but shows that the arrays are the same with no difference?
[英]phpunit fails asserting that two arrays are equal, but shows no difference
這是phpunit說的:
1) Asgard\Entity\Tests\EntityTest::testToArray
Failed asserting that two arrays are equal.
--- Expected
+++ Actual
@@ @@
Array (
'id' => null
'title' => 'Test Title'
'content' => 'Test Content'
'published' => 2015-03-04T11:19:50+0000
'comments' => Array (
0 => Array (
'id' => null
'content' => 'foo'
'published' => 2015-03-04T11:19:50+0000
'another_property' => null
'news' => null
)
1 => Array (
'id' => null
'content' => 'bar'
'published' => 2015-03-04T11:19:50+0000
'another_property' => null
'news' => null
)
2 => Array (
'id' => null
'content' => 'baz'
'published' => 2015-03-04T11:19:50+0000
'another_property' => null
'news' => null
)
)
'another_property' => null
)
https://travis-ci.org/asgardphp/asgard/jobs/53029084
“預期”和“實際”之間沒有區別。 測試通常會通過,但有時會失敗。
發現了這個問題。 元素的順序不一樣。 雖然輸出沒有顯示它有點煩人。
我用以下代碼替換了assertEquals:
$this->assertTrue($this->similar_arrays($arr1, $arr2));
protected function similar_arrays($a, $b) {
if(is_array($a) && is_array($b)) {
if(count(array_diff(array_keys($a), array_keys($b))) > 0)
return false;
foreach($a as $k => $v) {
if(!$this->similar_arrays($v, $b[$k]))
return false;
}
return true;
}
else
return $a === $b;
}
由於你在測試中使用了DateTime - 你確定你沒有在一秒內開始測試,而在另一個測試中完成測試,所以時間戳有第二個不同,這取決於你看哪個階段? - 在測試中使用動態日期和時間是偶爾測試失敗的常見原因。
檢查您的值中是否有EOL。
如果您有這樣的值:
'x' => 'line1
2' 號線
改為
'x' => "line1\nline2"
注意:這可能無法回答OP的直接問題,但這對我有用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.