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); }這些情況的解決方案是直接建立我的類別的模擬實例,並將建構函數所需的所有參數作為依賴項或資料。然後使用模擬類別作為真實類別繼續測試循環。