使用 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

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈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服务器地…