在開發與外部服務、API 或復雜功能交互的應用程序時,測試幾乎總是很困難。簡化測試的一種方法是使用存根類。以下是我通常使用它們的方法。
福利簡介
存根是接口或類的偽實現,用于模擬真實服務的行為。它們允許您:
無需調用外部服務即可測試代碼
無需 API 密鑰即可在本地工作
通過避免昂貴的 API 調用來加速測試
創建可預測的測試場景
外部會計服務示例
讓我們看一個外部會計服務的簡單接口。實際上,你甚至不需要接口來實現這一點,但它可以更輕松地切換實現并保持同步。
interface ExternalAccountingInterface
{public function createRecord(array $data): string;
}
以下是調用外部 API 的實際實現:
class ExternalAccounting implements ExternalAccountingInterface
{public function __construct(private readonly HttpClient $client,private readonly string $apiKey,) {}public function createRecord(array $data): string{$response = $this->client->post("https://api.accounting-service.com/v1/records", ['headers' => ['Authorization' => "Bearer {$this->apiKey}",'Content-Type' => 'application/json',],'json' => $data,]);$responseData = json_decode($response->getBody(), true);return $responseData['record_id'];}
}
現在,這里有一個用于測試的虛假實現:
class FakeExternalAccounting implements ExternalAccountingInterface
{private array $createdRecords = [];private bool $hasEnoughCredits = true;public function createRecord(array $data): string{if (! $this->hasEnoughCredits) {throw new InsufficientCreditsException("Not enough credits to create a record");}$recordId = Str::uuid();$this->createdRecords[$recordId] = $data;return $recordId;}// Edge case simulationpublic function withNotEnoughCredits(): self{$this->hasEnoughCredits = false;return $this;}// Helper methods for assertionspublic function assertRecordsCreated(array $eventData): void{Assert::assertContains($eventData,$this->createdRecords,'Failed asserting that the record was created with the correct data.');}public function assertNothingCreated(): void{Assert::assertEmpty($this->createdRecords, 'Records were created unexpectedly.');}
}
之前和之后:重構以使用存根
之前:使用 Mockery
public function testCreateAccountingRecord(): void
{// Create a mock using Mockery$accountingMock = $this->mock(ExternalAccountingInterface::class);// Set expectations$accountingMock->shouldReceive('createRecord')->once()->with(Mockery::on(function ($data) {return isset($data['type']) && $data['type'] === 'invoice' &&isset($data['amount']) && $data['amount'] === 99.99;}))->andReturn('rec_123456');// Bind the mock$this->swap(ExternalAccountingInterface::class, $accountingMock);// Execute the test$response = $this->post('/api/invoices', ['product_id' => 'prod_123','amount' => 99.99,]);// Assert the response$response->assertStatus(200);$response->assertJson(['success' => true]);
}
之后:使用存根
public function testCreateAccountingRecord(): void
{// Create an instance of our custom stub$fakeAccounting = new FakeExternalAccounting;// Bind the stub$this->swap(ExternalAccountingInterface::class, $fakeAccounting);// Execute the test$response = $this->post('/api/invoices', ['product_id' => 'prod_123','amount' => 99.99,]);// Assert the response$response->assertStatus(200);$response->assertJson(['success' => true]);// Assert that records were created with the expected data$fakeAccounting->assertRecordsCreated(['type' => 'invoice','amount' => 99.99,]);
}
自定義存根可以輕松測試邊緣情況和錯誤場景:
public function testInvoiceFailsWhenNotEnoughCredits(): void
{// Create an instance of our custom stub$fakeAccounting = new FakeExternalAccounting;// Configure the stub to simulate not enough credits$fakeAccounting->withNotEnoughCredits();// Bind the stub$this->swap(ExternalAccountingInterface::class, $fakeAccounting);// Execute the test expecting a failure$response = $this->post('/api/invoices', ['product_id' => 'prod_123','amount' => 99.99,]);// Assert the response handles the failure correctly$response->assertStatus(422);$response->assertJson(['error' => 'Insufficient credits']);// Assert that no records were created$fakeAccounting->assertNothingCreated();
}
通過此設置,您的本地開發環境將使用虛假實現,讓您無需 API 密鑰即可工作,也不用擔心速率限制。當部署到暫存區或生產環境時,應用程序將使用真實的實現
查看