Skip to Content
テスト駆動開発を学ぶ
book

テスト駆動開発を学ぶ

by Saleem Siddiqui
May 2025
Intermediate to advanced
280 pages
3h 35m
Japanese
O'Reilly Media, Inc.
Content preview from テスト駆動開発を学ぶ

第10章 エラー処理 エラー処理

この作品はAIを使って翻訳されている。ご意見、ご感想をお待ちしている:translation-feedback@oreilly.com

どのようなエラーが私たちの目と耳を狂わせるのだろうか?

ウィリアム・シェイクスピア(シラクサのアンティフォラスの舌を通して)『エラーの喜劇

ミスは人生の一部である。 テスト駆動開発を採用する理由のひとつは、コードのバグを最小限に抑えながら、安全にできる限り速く進めるようにすることだ。

機能リストの次の項目は、エラー処理の改善だ:

5米ドル×2=10米ドル

10ユーロ×2=20ユーロ

4002 krw / 4 = 1000.5 krw

5米ドル+10米ドル=15米ドル

テストコードを本番コードから分離する

冗長性テストを削除する

5米ドル+10ユーロ=17米ドル

1 USD + 1100 KRW = 2200 KRW#.

関係する通貨に基づいて為替レートを決定する(→から)。

為替レートが指定されていない場合のエラー処理を改善する。

為替レートの変更を許可する

エラー希望リスト

現在、私たちのコードが為替レートの欠落を処理する方法はバグだらけだ。この欠点に対処しよう。 表10-1は、為替レートの欠落によるエラー処理に関する我々のウィッシュリストである。

表10-1. 為替レートの欠落によるエラー処理に関する要望リスト
項目 説明

1

Evaluate メソッドは、必要な為替レートが1つ以上欠けている場合、明示的にエラーを知らせるべきである。

2

エラーメッセージは "貪欲 "であるべきだ。つまり、最初に欠落した為替レートだけでなく、Portfolio の評価を妨げる欠落した為替レートをすべて示すべきである。

3

呼び出し元によってエラーが無視されるのを防ぐため、為替レートの欠落が原因でエラーが発生した場合は、有効なMoney

インスタンスンス、通貨 "Kalganid "でポートフォリオを評価しようとする。1という通貨でポートフォリオを評価しようとすると、定義されていない為替レートがすべてリストアップされた詳細なエラーメッセージが表示される。

Go

為替レートが足りない場合は、convertEvaluate メソッドのシグネチャを変更する必要がある。 現在、これらのメソッドからは1つの値しか戻ってこない。エラー(為替レートが発見できないこと)を示すには、2つ目の戻り値が必要だ。

チップ

Goでは、失敗を示す慣用的な方法として、関数やメソッドの最後の戻り値としてerror

以下は、Evaluateconvert が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: ...
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

はじめての知識グラフ構築ガイド

はじめての知識グラフ構築ガイド

Jesus Barrasa, Jim Webber
初めてのGraphQL ―Webサービスを作って学ぶ新世代API

初めてのGraphQL ―Webサービスを作って学ぶ新世代API

Eve Porcello, Alex Banks, 尾崎 沙耶, あんどうやすし
ユーザーストーリーマッピング

ユーザーストーリーマッピング

Jeff Patton, 川口 恭伸, 長尾 高弘

Publisher Resources

ISBN: 9798341650992