Bry*_* M. 36 php phpunit unit-testing mocking
有时在我的代码中,我会检查一个特定的对象是否实现了一个接口:
if ($instance instanceof Interface) {};
Run Code Online (Sandbox Code Playgroud)
但是,在PHPUnit中创建所述接口的模拟,我似乎无法通过该测试.
// class name is Mock_Interface_431469d7, does not pass above check
$instance = $this->getMock('Interface');
Run Code Online (Sandbox Code Playgroud)
我知道有一个名为Interface的类与实现Interface的类不同,但我不知道如何处理它.
我是否被迫模拟实现Interface的具体类?难道这不会破坏使用接口进行移植的目的吗?
谢谢
Bre*_*zen 47
从3.5.0开始也有assertInstanceOf
例:
$this->assertInstanceOf('\Models\User', $this->userService->findById(1));
Run Code Online (Sandbox Code Playgroud)
小智 40
这对我有用:
$mock = $this->getMock('TestInterface');
$this->assertTrue($mock instanceof TestInterface);
Run Code Online (Sandbox Code Playgroud)
也许这是一个错字或者也许$ instance不是你认为的那样?