Skip to Content
Learning Test-Driven Development
book

Learning Test-Driven Development

by Saleem Siddiqui
October 2021
Intermediate to advanced
277 pages
5h 48m
English
O'Reilly Media, Inc.
Content preview from Learning Test-Driven Development

Chapter 2. Multicurrency Money

Followed fast and followed faster

Edgar Allen Poe, The Raven

Did the red-green-refactor cycle we followed in Chapter 1 seem a tad too slow?

A response of “Heck yes!” (or some rhyming phrase) is understandable!

The goal of test-driven development isn’t to force us to go slow—or fast, for that matter. Its goal is to allow us to go at a pace we’re comfortable with: speeding up when we can, slowing down when we should.

In this chapter, we’ll introduce additional currencies and the ability to multiply and divide money in any currency. Let’s see if we can kick up the pace a bit.

Enter the Euro

The second item on our list of features introduces a new currency:

5 USD × 2 = 10 USD

10 EUR × 2 = 20 EUR

4002 KRW / 4 = 1000.5 KRW

5 USD + 10 EUR = 17 USD

1 USD + 1100 KRW = 2200 KRW

This indicates that we need a more general entity than the Dollar we created in the previous chapter: something like Money, which encapsulates an amount (which we already have) and a currency (which we do not yet have). Let’s write tests to flesh out this new feature.

Go

Let’s write a new test in money_test.go. This test requires that when a struct representing “10 EUR” is multiplied by 2, we get “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 ...
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

Learning Domain-Driven Design

Learning Domain-Driven Design

Vlad Khononov
Learning Go

Learning Go

Jon Bodner
Learning React, 2nd Edition

Learning React, 2nd Edition

Alex Banks, Eve Porcello

Publisher Resources

ISBN: 9781098106461Errata Page