我对如何开始测试抽象类感到困惑。
一个例子:
abstract class Command
{
private $params;
public function with(array $params = [])
{
$this->params = $params;
}
public function getParams()
{
return $this->params;
}
abstract public function run();
}
我应该像这样测试它吗
/** @test */
public function is_an_abstract_class()
{
$command = $this->getReflectionClass();
$this->assertTrue($command->isAbstract());
}
/** @test */
public function has_an_run_method()
{
$command = $this->getReflectionClass();
$method = $this->getReflectionMethod('run');
$this->assertTrue($command->hasMethod('run'));
$this->assertTrue($method->isAbstract());
$this->assertTrue($method->isPublic());
$this->assertEquals(0, $method->getNumberOfParameters());
}
答案 0 :(得分:1)
我不应该测试抽象类吗?
在大多数情况下,这是我的选择。
原因1:某些类从抽象类继承的事实是实现细节,而不是行为。我们不想将测试与实现细节结合起来。
原因#2:我希望抽象类中的代码可以被覆盖其子孙的测试所覆盖。
如果您的设计正在“首先测试”中出现,那么您已经具有了这段代码的知识,因为抽象类将是您可以通过重构已经在测试中的类而引入到设计中的。