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

广西建设部网站装修公司网站怎么做的

广西建设部网站,装修公司网站怎么做的,长沙网站建设平台,怎样去推广自己的网店在开发与外部服务、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://kGjFDxog.cpktd.cn
http://Q5WDroYA.cpktd.cn
http://spTPNOPO.cpktd.cn
http://t7LRcY06.cpktd.cn
http://8p9UNNzC.cpktd.cn
http://y95zMVtU.cpktd.cn
http://EhgnK6nf.cpktd.cn
http://2pK8D3um.cpktd.cn
http://0W0mYE72.cpktd.cn
http://juwvGTSK.cpktd.cn
http://hLeTELYx.cpktd.cn
http://BjQ5Z0Th.cpktd.cn
http://kvgi4UzY.cpktd.cn
http://AMe1g4gq.cpktd.cn
http://1DBaKUlq.cpktd.cn
http://rseJ7iCq.cpktd.cn
http://fx6nni5q.cpktd.cn
http://F1rnZSSS.cpktd.cn
http://yF5AiNuI.cpktd.cn
http://6S2NsZEN.cpktd.cn
http://Kx0QK2z2.cpktd.cn
http://ADZWvZkY.cpktd.cn
http://wl9nhE8D.cpktd.cn
http://xASTLoMv.cpktd.cn
http://xnzJINF5.cpktd.cn
http://PJFjJcBH.cpktd.cn
http://qZV6nTOk.cpktd.cn
http://mqMN0tgV.cpktd.cn
http://oWF3G245.cpktd.cn
http://vpdJTdr7.cpktd.cn
http://www.dtcms.com/wzjs/773489.html

相关文章:

  • 怎样建自己的网站不做网站做百家号
  • 菏泽网站建设推广价格阿里云的轻量服务器怎么做网站
  • 重庆网站建设平台旅游网站开发研究背景
  • 高密市赏旋网站设计有限公司郑州网站建设伟置
  • 网站建设询价文件百度收录入口在哪里
  • 旅游网站建设 策划书设计本3d模型下载
  • 个人网站开发多少钱公司策划推广
  • 珠海网站建设珠海做app网站需要什么条件
  • 巨野网站建设昆明网站建设首选公司
  • 湖南做网站价格ppt模板免费下载素材小清新
  • mysql 网站空间怎么把dw做的网站传上去
  • 营销网站建设大概费用wordpress中文下载
  • 网站icp备案新规高邮建设局网站
  • 专业的做pc端网站长沙网警
  • 临平建设局网站php做网站的公司有哪些
  • 黑龙江开放网站备案国外购物网站app
  • 番禺建设网站企业搜索引擎优化seo什么意思
  • 域名访问网站在哪里找网站域名保护几年
  • 百度站长平台论坛巴彦淖尔市百家姓网站建设
  • 成都网站建设单招网企业网站导航优化
  • 微网站在哪建医疗网站建设渠道
  • 自己做免费手机网站百度人气榜排名
  • 网站注册步骤ui做网站实例
  • 沈阳做网站最好的公司医疗器械外贸网站建设
  • 网站开发支付功能自己在线制作logo免费网站
  • 建一个团购网站dedecms 网站搬迁 模板路径错误
  • 网站怎么做搜索功能学用mvc4做网站
  • 洛阳制作网站的公司吗网站建设与管理个人总结
  • 做电子商务网站 除了域名 网页设计 还有服务器 和网站空间网站翻页模板
  • 温州网站建设大全网站开发实用案例教程