当前位置: 首页 > wzjs >正文

wordpress 底部代码seo基础知识

wordpress 底部代码,seo基础知识,去哪接单做网站,兰州互联网公司在开发与外部服务、API 或复杂功能交互的应用程序时,测试几乎总是很困难。简化测试的一种方法是使用存根类。以下是我通常使用它们的方法。 福利简介 存根是接口或类的伪实现,用于模拟真实服务的行为。它们允许您: 无需调用外部服务即可测试…

在开发与外部服务、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.dtcms.com/wzjs/437902.html

相关文章:

  • 网站建设 简单动态网站搭建题库关键词首页优化
  • 深圳 做网站 互联百度快速排名点击器
  • 大连在哪里个省份seo项目
  • 哪里有做网站的seo整站优化推广
  • 手机编程软件哪个好西安seo网站关键词
  • 网站上可以做直播吗南昌做seo的公司有哪些
  • 天津企业网站建设一条龙seo有哪些优缺点?
  • 旅游网站开发目的和目标宁波seo排名公司
  • 网站后台的形成网站数据分析
  • 做网站建优化设计答案大全
  • office365做企业网站昆山网站建设推广
  • 麻涌镇做网站广东今天新闻最新消息
  • 深圳网站建设服务比较便宜百度广告竞价排名
  • 代做备案网站seo常用工具有哪些
  • 酷站百分百东莞seo项目优化方法
  • 快站wordpress百度seo排名优化公司哪家强
  • 油气集输毕业设计代做网站网络服务提供者不履行法律行政法规规定
  • 新郑市网站建设张雷明履新河南省委常委
  • 一个网站域名一年要多少钱为什么不能去外包公司
  • 推广公司服务内容宁波seo推广优化公司
  • 盱眙网站建设公司google首页
  • 套模版做网站磐石网站seo
  • 网页设计公司名称百度seo关键词优化电话
  • 北京做网站的公司哪家好seo网上培训多少钱
  • 具有品牌的广州做网站四川seo
  • 做网站分类链接网站页面的优化
  • 传统门户网站有哪些广州优化防控措施
  • 炫酷的个人网站百家号关键词seo优化
  • 葡萄城网站建设网站快速优化排名软件
  • 怎么用iapp做网站软件沈阳网站关键词优化公司