我有以下测试方法,用于测试构造函数的功能是否正常:
选项 #1
public function testConstructWorksProperly()
{
$id = 1;
$name = 'name';
$foo = new Foo($id, $name);
$this->assertEquals($id, $foo->getId());
$this->assertEquals($name, $foo->getNome());
}
选项#2
public function testConstructWorksProperly()
{
$id = 1;
$name = 'name';
$foo = new Foo($id, $name);
$this->assertAttributeEquals($id, 'id', $foo);
$this->assertAttributeEquals($name, 'name', $foo);
}
在选项#1中,我需要创建getter来断言构造函数是否正常工作,而在选项#2中,我使用一个断言来检查构造函数是否正确地设置了属性。
当我需要公开访问这些属性时,我想总是使用选项#1,因为这样可以节省时间和代码量,而不是编写另外两个测试用例用于getId和getName。
使用选项二似乎像是白盒测试。但是...
有一句话说:“每个测试一个断言”,因此,如果我的构造函数有6个参数,我将需要6个断言和6个getter来公开测试这些方法。
你会选择哪个选项?
Foo
的实例。 - Andrew