在一段遗留代码中,我的任务是测试如下特征中的静态函数:
namespace App\Model\SomeLogic;
trait WhyDecidedToUseTrait
{
public static function aMethodThatDoesSomeFancyStuff()
{
//Method Logic
}
}并使用getMockForTrait方法从这段documentation中。但在我的例子中,为了测试一个静态函数而创建一个虚拟对象是没有价值的,因为对象实例一开始就没有用。
此外,在使用此特性的对象中测试方法似乎非常耗时,而且进行更大规模的重构也很耗时。
那么,我如何测试特征,以便逐渐重构使用它的任何类呢?
发布于 2019-06-28 17:29:16
只需使用以下特征创建一个虚拟类:
namespace Tests\YourTeasts;
use PHPUnit\Framework\TestCase;
use App\Model\SomeLogic\WhyDecidedToUseTrait;
class Dummy
{
use WhyDecidedToUseTrait;
}
class StoreExtraAttributesTraitTest extends TestCase
{
public function setTheStaticMethod()
{
Dummy::aMethodThatDoesSomeFancyStuff();
//Assertions are done here
}
}因此,你可以测试这个方法,但是在覆盖率测试的情况下,我不知道什么时候会显示。
https://stackoverflow.com/questions/56804349
复制相似问题