[英]Set static method mock before instantiation in tests
I have an abstract class that calls a static method in it's constructor. 我有一个抽象类,在它的构造函数中调用静态方法。 However, when writing a test in PHPUnit, when I try to create a mock for it, it complains because the static method is abstract, and thus not defined when constructed.
但是,当我在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);
}
It gives the error Static method "createConnection" cannot be invoked on mock object
. 它给出了错误
Static method "createConnection" cannot be invoked on mock object
。
Please ignore that the class is called interface; 请忽略该类称为接口; it's one in function but poorly named and I can't think of how else to name it.
它是一个功能但命名不佳的我无法想到如何命名它。
静态方法不能被存根或模拟。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.