我有工厂
class DocumentManagerFactory
{
....
public function createDocumentManager(): DocumentManager
{
return DocumentManager::create($this->client, $this->configuration);
}
}
我想模拟DocumentManager
返回的createDocumentManager
$dmStub = $this->createMock(DocumentManager::class)
->method('refresh')
->willReturnArgument(1);
$dmFactoryStub = $this->createMock(DocumentManagerFactory::class)
->method('createDocumentManager')
->willReturn($dm);
我收到以下错误消息:
方法createDocumentManager可能不返回PHPUnit \ Framework \ MockObject \ Builder \ InvocationMocker类型的值,其返回声明为“:Doctrine \ ODM \ MongoDB \ DocumentManager”
有可能吗?
答案 0 :(得分:1)
这是因为您将调用链接到模拟,然后将这些调用分配给变量。如果将它们分开,应该可以正常工作。
$dmStub = $this->createMock(DocumentManager::class);
$dmStub->method('refresh')
->willReturnArgument(1);
$dmFactoryStub = $this->createMock(DocumentManagerFactory::class);
$dmFactoryStub->method('createDocumentManager')
->willReturn($dm);
在这种情况下,变量实际上保存了对象而不是调用。