【问题标题】:Set static method mock before instantiation in tests在测试中实例化之前设置静态方法模拟
【发布时间】:2019-04-24 01:19:27
【问题描述】:

我有一个抽象类,它在它的构造函数中调用一个静态方法。但是,当我在 PHPUnit 中编写测试时,当我尝试为它创建一个模拟时,它会抱怨,因为静态方法是抽象的,因此在构造时没有定义。

$interface = $this->getMockForAbstractClass(ConnectionAdapterInterface::class, [[]]);
$interface->method('createConnection')
    ->willReturn(new TestConnection());
$connection = $interface::createConnection();
abstract class ConnectionAdapterInterface
{

    public function __construct(array $configs) {
        $this->connection = static::createConnection($configs);
    }

    abstract public static function createConnection(array $config);

}

它给出了错误Static method "createConnection" cannot be invoked on mock object

请忽略该类称为接口;它是一种功能,但名称不佳,我想不出其他名称。

【问题讨论】:

    标签: php phpunit


    【解决方案1】:

    静态方法不能被存根或模拟。

    【讨论】:

    • 哦...所以我无法测试该方法,是吗?我尝试制作一个扩展该抽象的测试类,但 PHPUnit 不断给我错误。
    猜你喜欢
    • 1970-01-01
    • 2017-05-04
    • 1970-01-01
    • 1970-01-01
    • 2018-12-23
    • 1970-01-01
    • 2017-10-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多