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

テスト駆動開発を学ぶ

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

第2章 多通貨マネー

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

速くついて、速くついていく

エドガー・アレン・ポー『ワタリガラス

第1章で行ったレッド・グリーン ・リファクターのサイクルは、遅すぎると感じただろうか?

ヘック・イェス!」(あるいは韻を踏んだフレーズ)というレスポンスは理解できる!

テスト駆動開発のゴールは、無理に遅くすることでも、速くすることでもない。テスト駆動開発のゴールは自分たちが納得できるペースで開発を進めることだ。

この章では、追加の通貨と、任意の通貨でお金を掛けたり割ったりする機能を紹介する。少しペースを上げてみよう。

ユーロに参入する

、2つ目の項目は新通貨の導入である:

5米ドル×2=10米ドル

10ユーロ×2=20ユーロ

4002 krw / 4 = 1000.5 krw

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

1 USD + 1100 KRW = 2200 KRW

これは、前章で作成したDollar よりも一般化したエンティティが必要であることを示している。Money のようなもので、amount (すでに持っている)とcurrency (まだ持っていない)をカプセル化するものである。この新しい機能を具体化するためのテストを書いてみよう。

Go

money_test.go で新しいテストを書いてみよう。このテストは、"10 EUR "を表す構造体が2倍されると "20 EUR "になることを要求する:

func TestMultiplicationInEuros(t *testing.T) {
    tenEuros := Money{amount: 10, currency: "EUR"}
    twentyEuros := tenEuros.Times(2)
    if twentyEuros.amount != 20 {
        t.Errorf("Expected 20, got: [%d]", twentyEuros.amount)
    }
    if twentyEuros.currency != "EUR" {
        t.Errorf("Expected EUR, got: [%s]", twentyEuros.currency)
    }
}

このテストでは、「10 EUR」と「20 EUR」の概念を、currencyamount を含むstruct インスタンスで表現する。

ここまでで、このテストを実行すると、次のようなエラーが出ることがわかった。 undefined: Money.これは、新しい構造体を導入することで解消できる:

type Money struct {
    amount   int
    currency string
}

type Money has no field or method Times というエラーが発生するが、これを回避する方法はわかっている。Money に対してTimes メソッドを定義する:

func (m Money) Times(multiplier int) Money {
    return Money{amount: m.amount * multiplier, currency: m.currency}
}

やった!また緑のテストだ。

JavaScript

Money オブジェクトをamountcurrency で表すテストを書いてみよう。10 EUR "を表すオブジェクトに2を掛けると ...

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