douyao1994 2019-06-28 09:29 采纳率: 100%
浏览 115
已采纳

PHPUnit测试位于特征中的静态函数

In a piece of legacy code I was tasked to test a static function in a trait like that:

namespace App\Model\SomeLogic;

trait WhyDecidedToUseTrait
{
   public static function aMethodThatDoesSomeFancyStuff()
   {
     //Method Logic
   }
}

And from this piece of documentation using the getMockForTrait method. But in my case making a dummy object in order to test a static function where object instants are useless to begin with has no value.

Also testing the method in objects that use this trait seems pretty much time consuming, also tdoing a larger scale refactoring is time consuming as well.

So how I can test the trait in order to gradually refactor any class that uses it?

  • 写回答

1条回答 默认 最新

  • doutong6814 2019-06-28 09:29
    关注

    Just make a Dummy class using this trait:

    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
       }
    }
    
    

    Hence you can test the method, but in case os coverage tests I have no idea whenther is shown or not.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog