douyao1994
2019-06-28 09:29
浏览 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?

图片转代码服务由CSDN问答提供 功能建议

在一段遗留代码中,我的任务是测试一个特征中的静态函数:

  namespace App \ Model \ SomeLogic; 
 
trait WhyDecidedToUseTrait 
 {
公共静态函数aMethodThatDoesSomeFancyStuff()
 {
 //方法逻辑
} 
} 
    
 
 

从这段 getMockForTrait 方法“rel =”nofollow noreferrer“>文档。 但是在我的情况下,为了测试一个静态函数来制作一个虚拟对象,其中对象瞬间开始没用,没有任何价值。

同样在使用这个特性的对象中测试方法看起来很漂亮 花费大量时间,同时进行更大规模的重构也是非常耗时的。

那么我如何测试特性以逐步重构任何使用它的类? \ n

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

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.

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题