Skip to Content
学习测试驱动开发
book

学习测试驱动开发

by Saleem Siddiqui
July 2025
Intermediate to advanced
280 pages
3h 5m
Chinese
O'Reilly Media, Inc.
Content preview from 学习测试驱动开发

第 10 章 错误处理 错误处理

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com

什么错误会让我们的眼睛和耳朵出错?

威廉-莎士比亚(通过锡拉库扎的安提福勒斯之口),《错误的喜剧

错误是生活的一部分。 采用测试驱动开发的原因之一就是确保我们能在安全的情况下尽可能快地开发,将代码中的错误降到最低。

我们功能列表上的下一个项目就是改进错误处理:

5 美元 × 2 = 10 美元

10 欧元 × 2 = 20 欧元

4002 韩元 / 4 = 1000.5 韩元

5 美元 + 10 美元 = 15 美元

将测试代码与生产代码分开

删除多余的测试

5 美元 + 10 欧元 = 17 美元

1 美元 + 1100 克鲁瓦 = 2200 克鲁瓦#。

根据涉及的货币确定汇率(从→到)

改进汇率未指定时的错误处理

允许修改汇率

错误愿望列表

我们的代码目前处理汇率缺失的方式存在缺陷。让我们来解决这个缺陷。 表 10-1列出了我们处理因汇率缺失而导致的错误的愿望清单。

表 10-1. 处理因汇率缺失而导致的错误的愿望清单
项目 说明

1

当缺少一个或多个必要的汇率时,Evaluate 方法应发出明确的错误信号。

2

错误信息应该是 "贪婪的"--也就是说,它应该指出所有导致Portfolio 无法求值的缺失汇率,而不仅仅是第一个缺失的汇率。

3

为防止错误被调用者忽略,当汇率缺失导致错误时,不应返回有效的Money 。

例如,如果我们尝试评估货币 "Kalganid "的投资组合1的投资组合时,我们应该得到一条详细的错误信息,列出所有缺失的汇率。

Go

当汇率缺失时,我们需要修改convert 和Evaluate 方法的签名。 目前,这些方法只返回一个值。为了显示错误(无法找到汇率),我们需要第二个返回值。

提示

在 Go 中,表示失败的惯用方法是返回一个error 作为函数或方法的最后一个返回值,以便调用者可以检查。

下面是Evaluate 和convert 如何使用 Go 的习语协同工作的伪代码:

Evaluate:
    For each Money struct:
        Try to convert Money to target currency and add it to the total amount
            If convert returns an error:
                Capture the "from" and "to" currencies in "failures"
    If there are no failures:
        Return a Money struct with the total amount and target currency;
          return nil for error
    Otherwise:
        Return an empty Money struct; return an error message
          including all the failures

有了这些伪代码,让我们在money_test.go 中编写一个失败的测试。该测试与现有测试略有不同:它期望返回错误,并将错误信息与预期信息进行比较:

func TestAdditionWithMultipleMissingExchangeRates(t *testing.T) {
    var portfolio s.Portfolio

    oneDollar ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

AI原生软件交付

AI原生软件交付

Nick Durkin, Eric Minick, Chinmay Gaikwad
生成式人工智能可视化

生成式人工智能可视化

Priyanka Vergadia, Valliappa Lakshmanan

Publisher Resources

ISBN: 9798341663183