使用 Laravel 中的自定義存根簡化工作

在開發與外部服務、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 密鑰即可工作,也不用擔心速率限制。當部署到暫存區或生產環境時,應用程序將使用真實的實現

查看

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/diannao/86173.shtml
繁體地址,請注明出處:http://hk.pswp.cn/diannao/86173.shtml
英文地址,請注明出處:http://en.pswp.cn/diannao/86173.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

將 tensorflow keras 訓練數據集轉換為 Yolo 訓練數據集

以 https://www.kaggle.com/datasets/vipoooool/new-plant-diseases-dataset 為例 1. 圖像分類數據集文件結構 (例如用于 yolov11n-cls.pt 訓練) import os import csv import random from PIL import Image from sklearn.model_selection import train_test_split import s…

排序算法-歸并排序與快速排序

歸并排序與快速排序 快速排序是利用的遞歸思想:選取一個基準數,把小于基準數的放左邊 大于的放右邊直到整個序列有序 。快排分割函數 O(lognn), 空間 :沒有額外開辟新的數組但是遞歸樹調用函數會占用棧內存 O(logn) 。 歸并排序:在遞歸返回的…

北大開源音頻編輯模型PlayDiffusion,可實現音頻局部編輯,比傳統 AR 模型的效率高出 50 倍!

北大開源了一個音頻編輯模型PlayDiffusion,可以實現類似圖片修復(inpaint)的局部編輯功能 - 只需修改音頻中的特定片段,而無需重新生成整段音頻。此外,它還是一個高性能的 TTS 系統,比傳統 AR 模型的效率高出 50 倍。 自回歸 Tra…

MyBatis————入門

1,配置相關 我們上一期詳細講了一下使用注解來實現操作數據庫的方式,我們今天使用xml來實現,有同學可能有疑問,使用注解挺方便呀,為啥還要注解呀,先來說一下注解我感覺挺麻煩的,但是我們后面要…

【推薦算法】推薦算法演進史:從協同過濾到深度強化學習

推薦算法演進史:從協同過濾到深度強化學習 一、傳統推薦時代:協同過濾的奠基(1990s-2006)1.1 算法背景:信息爆炸的挑戰1.2 核心算法:協同過濾1.3 局限性 二、深度學習黎明:神經網絡初探&#xf…

Java基于SpringBoot的校園閑置物品交易系統,附源碼+文檔說明

博主介紹:?Java老徐、7年大廠程序員經歷。全網粉絲12w、csdn博客專家、掘金/華為云/阿里云/InfoQ等平臺優質作者、專注于Java技術領域和畢業項目實戰? 🍅文末獲取源碼聯系🍅 👇🏻 精彩專欄推薦訂閱👇&…

Ajax Systems公司的核心產品有哪些?

Ajax Systems 是一家專注于家庭安全和智能系統的公司,其核心產品如下3: 入侵保護設備:如 MotionCam Outdoor 無線室外運動探測器,配備內置攝像頭和兩個紅外傳感器,可通過預裝電池運行長達三年,能在 15 米距…

64、js 中require和import有何區別?

在 JavaScript 中,require 和 import 都是用于模塊導入的語法,但它們屬于不同的模塊系統,具有顯著的區別: 1. 模塊系統不同 require 屬于 CommonJS 模塊系統(Node.js 默認使用)。 語法:const…

Java+Access綜合測評系統源碼分享:含論文、開題報告、任務書全套資料

JAVAaccess綜合測評系統畢業設計 一、系統概述 本系統采用Java Swing開發前端界面,結合Access數據庫實現數據存儲,專為教育機構打造的綜合測評解決方案。系統包含學生管理、題庫管理、在線測評、成績分析四大核心模塊,實現了測評流程的全自…

【python】RGB to YUV and YUV to RGB

文章目錄 1、YUV2、YUV vs RGB3、RGB to YUV4、YUV to RGB附錄——YUV NV12 vs YUV NV21參考1、YUV YUV 顏色空間,又常被稱作 YCbCr 顏色空間,是用于數字電視的顏色空間,在 ITU-R BT.601、BT.709、BT.2020 標準中被明確定義,這三種標準分別針對標清、高清、超高清數字電視…

運行示例程序和一些基本操作

歡迎 ----> 示例 --> 選擇sample CTRL B 編譯代碼 CTRL R 運行exe 項目 中 Shadow build 表示是否 編譯生成文件和 源碼是否放一塊 勾上不在同一個地方 已有項目情況下怎么打開項目 方法一: 左鍵雙擊 xxx.pro 方法二: 文件菜單里面 選擇打開項目

計算機網絡第2章(下):物理層傳輸介質與核心設備全面解析

目錄 一、傳輸介質1.1 傳輸介質的分類1.2 導向型傳輸介質1.2.1 雙絞線(Twisted Pair)1.2.2 同軸電纜(Coaxial Cable)1.2.3 光纖(Optical Fiber)1.2.4 以太網對有線傳輸介質的命名規則 1.3 非導向型傳輸介質…

PHP文件包含漏洞詳解:原理、利用與防御

PHP文件包含漏洞詳解:原理、利用與防御 什么是文件包含漏洞? 文件包含漏洞是PHP應用程序中常見的安全問題,當開發者使用包含函數引入文件時,如果傳入的文件名參數未經嚴格校驗,攻擊者就可能利用這個漏洞讀取敏感文件…

5.4.2 Spring Boot整合Redis

本次實戰主要圍繞Spring Boot與Redis的整合展開,首先創建了一個Spring Boot項目,并配置了Redis的相關屬性。接著,定義了三個實體類:Address、Family和Person,分別表示地址、家庭成員和個人信息,并使用Index…

java內存模型JMM

Java 內存模型(Java Memory Model,JMM)定義了 Java 程序中的變量、線程如何和本地內存以及主內存進行交互的規則。它主要涉及到多線程環境下的共享變量可見性、指令重排等問題,是理解并發編程中的關鍵概念。 核心概念&#xff1a…

配置git命令縮寫

以下是 Git 命令縮寫的配置方法及常用方案,適用于 Linux/macOS/Windows 系統: 🔧 一、配置方法 1. 命令行設置(推薦) # 基礎命令縮寫 git config --global alias.st status git config --global alias.co che…

準確--k8s cgroup問題排查

k8s cgroup問題排查 6月 06 17:20:39 k8s-node01 containerd[1515]: time"2025-06-06T17:20:39.42902033408:00" levelerror msg"StartContainer fo r \"46ae0ef9618b96447a1f28fd2229647fe671e8acbcec02c8c46b37051130c8c4\" failed" error&qu…

Go 中 map 的雙值檢測寫法詳解

Go 中 map 的雙值檢測寫法詳解 在 Go 中,if char, exists : pairs[s[i]]; exists { 是一種利用 Go 語言特性編寫的優雅條件語句,用于檢測 map 中是否存在某個鍵。讓我們分解解釋這種寫法: 語法結構解析 if value, ok : mapVariable[key]; …

C# Wkhtmltopdf HTML轉PDF碰到的問題

最近碰到一個Html轉PDF的需求,看了一下基本上都是需要依賴Wkhtmltopdf,需要在Windows或者linux安裝這個可以后使用。找了一下選擇了HtmlToPDFCore,這個庫是對Wkhtmltopdf.NetCore簡單二次封裝,這個庫的好處就是通過NuGet安裝HtmlT…

grafana 批量視圖備份及恢復(含數據源)

一、grafana 批量視圖備份 import requests import json import urllib3 import osfrom requests.auth import HTTPBasicAuthfilename_folders_map "folders_map.json" type_folder "dash-folder" type_dashboard "dash-db"# Grafana服務器地…