Chapter 4: Privacy
A benefit of TDD is having tests which document code behavior; this chapter starts by adapting an existing test to make it "speaking". We change our assertion to be based on an object of a particular value rather than a value separated from it's context and, at the same time inline some noisy declarations.
I like to use the IDE to help me refactor, so inlining the 'actual result' declaration for my AssertEquals
comparison using the shortcut ⌥ ⌘ N
(mac) / Ctrl+Alt+N
(windows) is a joyful thing to do!
An assertion of truth, not a sequence of operations
You will only have tests which document the code if you write them in such a way they are clearly understood. As you build functionality your test suite can be improved upon, but there is inherent risk in changing tests. Relying on the TDD process should give some confidence in moving forward whilst keeping the tests green.
And with regards to privacy...? The code change in this chapter allows us to make a public
instance variable private
.
🔎 View my code-along repo at https://github.com/ruthmoog/test-driven-development-by-example
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.