Chapter 2. Testing Ruby

 

This chapter covers

  • Testing principles
  • Test-driven development
  • Behavior-driven development
  • Using stubs and mocks
  • Testing your tests

How do you currently test your code? From what people tell us, it seems the most popular testing methodology is “works now, works later”: they test their software by playing around with it, and if it works now, they assume it always will. If this is your approach, you’ve surely encountered many of the problems we did when we used that method: changes to one part of the system breaking another, unexpected edge cases (or non-edge cases!) causing the software to behave unexpectedly, and so on. Then comes the inevitable debugging. The whole business is a huge time and effort sink.

You can avoid this entire situation and gain more assurances along the way by using automated testing. Ruby makes it easy by including a unit-testing library with its standard distribution (and more tools are available via third-party libraries). But before we use that library, let’s take a look at the basics of software testing.

2.1. Testing principles

2.2. Test-driven development with Ruby

2.3. Behavior-driven development with RSpec

2.4. A testing environment

2.5. Testing your tests

2.6. Summary