我对如何开始测试抽象类的方式感到困惑。我应该测试每种方法吗?我应该测试什么?我不应该测试抽象类吗?一个例子: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());}
1 回答
冉冉说
TA贡献1877条经验 获得超1个赞
我不应该测试抽象类吗?
在大多数情况下,这将是我的选择。
原因#1:某些类从抽象类继承的事实是实现细节,而不是行为。我们不想将我们的测试与实现细节结合起来。
原因#2:我希望抽象类中的代码被覆盖其后代的测试覆盖。
如果您的设计是新兴的“测试优先”,那么您已经涵盖了此代码,因为抽象类将是您将通过重构已经在测试中的类引入到您的设计中的东西。
- 1 回答
- 0 关注
- 250 浏览
添加回答
举报
0/150
提交
取消