Chapter 12. Testing

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 Rails’ basic testing setup and note some options for building more advanced test platforms. (Examples for this chapter are in ch12/students006.)

Note

Many developers use RSpec, an additional framework for testing noted at the end of this chapter, but it’s worth understanding the foundations provided in Rails itself.

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:

Development

Development is the default mode. In development mode, Rails loads and reloads code every time a request is made, making it easy for you to see changes without a cache getting in the way. It’s also typical to use SQLite as the database, as Rails isn’t going to be working at high speed anyway.

Test

Test mode runs like production mode, without reloading code, and has its own database so that tests can run against a consistent database. You could use a fancier database for test mode (and might want to if you suspect strange database interactions), but for getting started, the default of SQLite is fine.

Production

Production mode maximizes Rails’ efficiency. It doesn’t reload code, enabling it to cache the program and run much faster. Logging is much briefer and error messages are shortened, as giving users a complete stack trace probably isn’t helpful. It also does more automatic and directed caching of results, sparing users a wait for the same code to run again.

You can switch among the three modes by using the -e option of script/server:

script/server -e production

The settings for all three modes are in the config directory. The environment.rb file contains default configuration settings used by all three modes, but the environments directory contains development.rb, production.rb, and test.rb files whose settings override those in environment.rb.

The database.yml file contains the database connection settings for all three modes. By default, it specifies SQLite databases named db/development.sqlite3, db/test.sqlite3, and db/production.sqlite3. Chapter 18 will explore other possible database installations, particularly for production, but for now, these defaults are fine. It’s time, though, to set up a database for testing.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset