計算響應和正確答案之間的差異,忽略任何過多的值。如果沒有區別,一切都很好;如果有的話,你會得到完整的信息。
//some examples
$responseValues = array('success' => true, 'user_ip' => '212.20.30.40', 'id' => '7629428643'); //success
$errorResponseValues = array('success' => false, 'user_ip' => '212.20.30.40', 'id' => '7629428643'); //failure
$errorResponseValues2 = array('success' => false, 'user_ip' => '212.20.30.40', 'id' => '123'); //failure
$expectedValues = array('success' => true, 'id' => '7629428643'); //what is success
function whatIsWrong($reality, $expectation)
{
return array_uintersect_assoc($reality, $expectation, function($a, $b){return (int)($a == $b);}); //This is slightly dirty, I think the final implementation is up to you
}
var_dump(whatIsWrong($responseValues, $expectedValues)); //array()
var_dump(whatIsWrong($errorResponseValues, $expectedValues)); //array('success' => false)
var_dump(whatIsWrong($errorResponseValues2, $expectedValues)); //array('success' => false, id => 123)
然後你可以使用assertEqual便(whatIsWrong(...),陣列()),這應該輸出在失敗的差別,或者處理它在幾乎所有的首選方式。
這對我來說看起來很乾淨。 – Matt 2012-08-03 13:49:04
該解決方案只能告訴你測試失敗,但不能告訴你究竟出了什麼問題,這就是爲什麼我認爲這是「髒」的原因。 – 2012-08-03 13:54:07