ユニットテストでしばらく遊んでいるという事実にもかかわらず、単一の機能である「ユニット」の概念を実際には理解できません。
たとえば、次の形式で魔法のメソッドのグループをテストしていますnewXxx
。
public function testMagicCreatorWithoutArgument()
{
$retobj = $this->hobj->newFoo();
// Test that magic method sets the attribute
$this->assertObjectHasAttribute('foo', $this->hobj);
$this->assertInstanceOf(get_class($this->hobj), $this->hobj->foo);
// Test returned $retobj type
$this->assertInstanceOf(get_class($this->hobj), $retobj);
$this->assertNotSame($this->hobj, $retobj);
// Test parent property in $retobj
$this->assertSame($this->hobj, $retobj->getParent());
}
ご覧のとおり、このテスト メソッドにはアサーションの 3 つの「グループ」があります。「単体テスト」の原則に従うために、それらを3つの単一のテスト方法に分割する必要がありますか?
分割は次のようになります。
public function testMagicCreatorWithoutArgumentSetsTheProperty()
{
$this->hobj->newFoo();
$this->assertObjectHasAttribute('foo', $this->hobj);
$this->assertInstanceOf(get_class($this->hobj), $this->hobj->foo);
}
/**
* @depends testMagicCreatorWithoutArgumentReturnsNewInstance
*/
public function testMagicCreatorWithArgumentSetsParentProperty()
{
$retobj = $this->hobj->newFoo();
$this->assertSame($this->hobj, $retobj->getParent());
}
public function testMagicCreatorWithoutArgumentReturnsNewInstance()
{
$retobj = $this->hobj->newFoo();
$this->assertInstanceOf(get_class($this->hobj), $retobj);
$this->assertNotSame($this->hobj, $retobj);
}