Testing First Drives Design

Success with test-driven development starts with trusting the process. The classic process goes like this:

  1. Create a test. The test should be short and test for one thing in your code. The test should run automatically.

  2. Make sure the test fails. Verifying the test failure before you write code helps ensure that the test really does what you expect.

  3. Write the simplest code that could possibly make the test pass. Don’t worry about good code yet. Don’t look ahead. Sometimes, write just enough code to clear the current error.

  4. After the test passes, refactor to improve the code. Clean up duplication. Optimize. Create new abstractions. Refactoring is a key part of design, so don’t skip this. Remember to run the tests ...

Get Rails 4 Test Prescriptions now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.