【发布时间】:2020-04-18 22:53:48
【问题描述】:
使用 PHPUnit,我如何正确测试构造函数(对于下面简化示例中的 Built 类)是否被正确调用? Container 类是被测试的类。我希望能够在下面的数组键中捕捉到诸如故意错字之类的东西。不可能模拟构造函数,否则这将是直截了当的。
class Container {
public function create(string $input) {
$request = new Built(["rid" => $input]); // Oops, typo in keyname.
}
}
class Built {
private $tid;
public function __construct(array $params) {
$this->tid = $params["tid"];
}
}
这与How to unit test the methods of a class whose constructor take some arguments? 中描述的情况不同,因为我的测试类正在实例化第二个类。
【问题讨论】:
-
不,它没有,因为我的测试类正在实例化第二个类,而不是使用注入的类。