Heim > Fragen und Antworten > Hauptteil
P粉8850351142023-09-03 17:16:49
好吧,我找到了解决方案,但无论如何,我有兴趣知道是否有人也遇到这个问题
我解决这个问题的方法是:
public function testRealExternalMethod() { // Create a mock of the dependency that will be used by the class $mockDependency = $this->createMock(Dependency::class); // in this case we need to use a partial mock because we have an internal method $mockMyClass = Mockery::mock( MyClass::class, [$mockDependency, $parameter1] ) ->makePartial(); $responseInternalMethod = ['needed data']; $mockMyClass->shouldReceive('internalMethod') ->andReturn($responseInternalMethod); $result = $mockMyClass->realExternalMethod($parameter2, $parameter3); }这些情况的解决方案是直接创建我的类的模拟实例,并将构造函数所需的所有参数作为依赖项或数据。然后使用模拟类作为真实类继续测试循环。