Chapter 12. Testing

Failures, repeated failures, are finger posts on the road to achievement. One fails forward toward success.

C. S. Lewis

Testing can spare you much of the work you learned to do in the previous chapter, replacing spot-check debugging with more structured and thorough repetitive testing. Ruby culture places a high value on testing, and Ruby and Rails have grown up with agile development methods where testing is assumed to be a normal part of development. While testing is a complicated subject worthy of a book or several, it’s definitely worthwhile to start including tests early in your project development, even while you’re still learning the Rails landscape.

Rails provides a number of facilities for creating and managing tests. This chapter will explore its basic testing setup and note some options for building more advanced test platforms. (Examples for this chapter are in ch12/students06.)

Note

As you get deeper into Rails culture, you’ll find many people using other testing frameworks, notably RSpec, Cucumber, and Capybara, with Factory Girl managing fixtures. It’s probably easiest, though, to start by understanding the foundations provided in Rails itself and then moving on when you need more sophisticated features or a particular approach.

Test Mode

Up to this point, all the code in this book has been run in development mode. Rails supports three different environments for running applications. Each of them has its own database, as well as its own settings: ...

Get Learning Rails 5 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.