What We Just Learned

Testing applications that use databases can be difficult. You have to make sure each scenario starts with a clean slate, but cleaning the slate can have its own pitfalls. In this chapter, we learned about the benefits and drawbacks of two strategies—transaction and truncation. To sum it up:

  • ActiveRecord is a useful library for quickly connecting to SQL databases.

  • Resetting state between scenarios is vital; otherwise, you get weird failures.

  • Transaction-based cleaning is preferred because it is fast, but it works only when there is one single-threaded process.

  • Truncation-based cleaning is a slower, brute-force technique that works in multiprocess and multithreaded environments.

Try This

Find an existing database, perhaps somewhere on your network at work, and see if you can connect ActiveRecord to it. It should only take you a few lines of Ruby code to set up an ActiveRecord::Base subclass that can talk to one of the tables in the schema. Read up on ActiveRecord’s API documentation[51] and try running some queries against the database. See how easy it is to use Ruby to talk to any database!

Footnotes

[45]

http://rubygems.org/gems/activerecord

[46]

http://www.sqlite.org/

[47]

At the time of this writing, ActiveRecord has drivers for MySQL, PostgreSQL, SQLite, SQL Server, Sybase, DB2, and Oracle databases.

[48]

The ActiveRecord library is based on the design pattern of the same name: http://en.wikipedia.org/wiki/Active_record_pattern

[49]

http://api.rubyonrails.org/classes/ActiveRecord/Migration.html

[50]

DatabaseCleaner (https://github.com/bmabey/database_cleaner) was written by Ben Mabey, who was also one of the first contributors to Cucumber.

[51]

http://api.rubyonrails.org/classes/ActiveRecord/Base.html

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

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