public function test_cleansUpHouses()
{
$houseWithNoHP = new \DeadStreet\ValueObject\House\House();
$houseWithNoHP->setHitPoints(0);
$houseWithNoHP->setCurrentAttackers(1);
$houseWithHP = new \DeadStreet\ValueObject\House\House();
$houseWithHP->setCurrentAttackers(1);
$houseWithHPWasNotAttacked = new \DeadStreet\ValueObject\House\House();
$houseWithHPWasNotAttacked->setCurrentAttackers(1);
$houseCollection = new \DeadStreet\ValueObject\House\Collection(
[$houseWithNoHP, $houseWithHP, $houseWithHPWasNotAttacked]
);
$this->mockHouseModel->expects($this->at(0))
->method('hasBeenAttacked')
->with($houseWithNoHP)
->willReturn(true);
$this->mockHouseModel->expects($this->at(1))
->method('hasBeenAttacked')
->with($houseWithHP)
->willReturn(true);
$this->mockHouseModel->expects($this->at(2))
->method('hasBeenAttacked')
->with($houseWithHPWasNotAttacked)
->willReturn(false);
$this->mockHouseModel->expects($this->at(0))
->method('requiresDestroying')
->with($houseWithNoHP)
->willReturn(true);
$this->mockHouseModel->expects($this->at(1))
->method('requiresDestroying')
->with($houseWithHP)
->willReturn(false);
$expectedHouses = [$houseWithHP, $houseWithHPWasNotAttacked];
$cleanedUpHouses = $this->createObject()->cleanUpHouses($houseCollection);
$this->assertEquals($expectedHouses, $cleanedUpHouses->getHouses());
}
private function createObject()
{
return new Collection($this->mockHouseModel);
}
这是测试中的模型
public function cleanUpHouses(\DeadStreet\ValueObject\House\Collection $collection)
{
foreach($collection->getHouses() as $key => $house) {
if(!$this->houseModel->hasBeenAttacked($house)) {
break;
}
if($this->houseModel->requiresDestroying($house)) {
unset($collection->getHouses()[$key]);
}
}
return $collection;
}
但是,即使我有一行
,此行if($this->houseModel->requiresDestroying($house)) {
也永远不会返回true
$this->mockHouseModel->expects($this->at(0))
->method('requiresDestroying')
->with($houseWithNoHP)
->willReturn(true);
我得到的错误。
1) DeadStreet\Model\House\Collection_Test::test_cleansUpHouses
Failed asserting that two arrays are equal.
--- Expected
+++ Actual
@@ @@
Array (
0 => DeadStreet\ValueObject\House\House Object (...)
1 => DeadStreet\ValueObject\House\House Object (...)
+ 2 => DeadStreet\ValueObject\House\House Object (...)
)
答案 0 :(得分:0)
您确定代码是否正常运行?
这一行
unset($collection->getHouses()[$key]);
使用数组的副本来执行取消设置。
示例:
class Test
{
private $array = array("a" => 1, "b" => 2);
public function unsetArray()
{
unset($this->getArray["a"]);
}
public function getArray()
{
return $this->array;
}
}
$test = new Test();
$test->unsetArray();
方法unsetArray
总是在这里复制副本,而不是实际的数组。如果你想要那样的话。你必须像这样使用实际的数组。
public function unsetArray()
{
unset($this->array["a"]);
}