PHPUnit - 模拟一个特征

Pab*_* V. 4 php phpunit prophecy

我有一个被多个类使用的特征,即

class SomeClass
{
     use TimeoutTrait;

     function handle() {
         $this->traitFunction()    // can this be mocked?
     }
}
Run Code Online (Sandbox Code Playgroud)

PHP 单元能够模拟 traitFunction()?。

在此先感谢您的帮助。

你好

Pau*_*een 6

Traits 是代码容器,编译器将代码“复制粘贴”到您要在其中使用的类中,从而使其可在全世界重复使用。

通常,单元测试中的特征函数没有什么特别之处,bec。当类被实例化时,trait 函数与可以直接复制粘贴的函数没有任何区别,它们被写入类中的 trait。

因此,不要犹豫,以与类中定义的任何其他常规函数相同的方式模拟您的 trait 函数