有没有办法用嘲弄来模拟私有静态方法?

Is there any way to mock private static method with mockery?

例如我有一个 class SampleService 如下所示,现在我如何使用 PHPUnit 为 getName() 方法编写单元测试?更具体地说,我如何使用 Mockery 来模拟 self::_getName();?如果那不可能,什么是最好的 approach/way 来为这种场景编写单元测试?谢谢

class SampleService implements Service
{    
    public function getName(){
        $name = self::_getName();
        return 'Mr. '.$name;
    }

    private static function _getName(){
        return 'Some Name';
    }
}

不,您不应该在 TDD 中使用它们。仅当您永远不需要模拟它时才使用它们。

如果您需要模拟它 - 将所有代码移至新的 class 并模拟新的 class.