Book Club: "Test Driven Development: By Example" #5

ruthmoog - Apr 22 '22 - - Dev Community

Chapter 5: Franc-ly Speaking

The key problem to be solved in this part of the book, is a multi-currency sum,

$5 + 10CHF = $10
Enter fullscreen mode Exit fullscreen mode

Based on the real-life example from the book's introduction, we're working with US Dollars and Swiss Francs.

This chapter lets us break down this problem, because it's not possible to test everything that needs to happen to solve this sum in one test.

The way we wrote our previous test for multiplication means it can be reused for Francs instead of Dollars. We can rush our test to green by just copying the implementation for Dollar, and this feels to me like polymorphism: we have these two currencies which have different properties but behave in essentially the same way. How exciting! But to avoid getting ahead of myself, write a test; make it compile; watch it fail; make it pass; ✨refactor✨.

One thing I found tricky here was, how could I use the IDE to copy the code for me - if I tried to do a refactor here then I don't want to change my existing Dollar tests and class. I ended up copy/pasting and manually changing the occurrences of Dollar and dollar to Franc and franc, but I didn't feel good about it. I also have tests for francs and dollars in the same test class (which might be fine, except I called my test class DollarTest).

For now, I'll rename my tests to MoneyTest which is a better description of its content, until I know more about my design.

🔎 View my code-along repo at https://github.com/ruthmoog/test-driven-development-by-example


Book cover Kent Beck's "Test Driven Development: By Example" was released in 2002. The book aims to explain how to use TDD to write quality code that works.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .