有时在我的代码中,我会检查特定对象是否实现了一个接口:
if ($instance instanceof Interface) {};
不过,在PHPUnit中创建该接口的模拟时,我似乎无法通过该测试。
// class name is Mock_Interface_431469d7, does not pass above check
$instance = $this->getMock('Interface');
我知道起名为Interface的类与实现Interface接口的类是不同的,但我不确定如何处理这个问题。
我必须模拟一个实现Interface接口的具体类吗?那样岂不是会失去使用接口实现可移植性的目的吗?
谢谢
failed asserting that object is instance of Class
与fail asserting that false is true
进行比较。 - Mike Doe\Models\User::class
代替'\Models\User'
,这使得重构变得更加容易。 - Vegard Larsen