ProblemYou need to make sure that your application is interacting with the database correctly (creating, reading, updating, and deleting records). Your data model is the foundation of your application, and keeping it error free can go along way toward eliminating bugs. You want to write tests to verify that basic CRUD operations are working correctly. SolutionAssume you have a table containing books, including their titles and ISBNs. The following migration sets up this table: db/migrate/001_create_books.rb: class CreateBooks < ActiveRecord::Migration def self.up create_table :books do |t| t.column :title, :string t.column :isbn, :string end end def self.down drop_table :books end end After creating the books table by running this migration, you need to set up your test database. Do this by running the following rake command: $ rake db:test:clone_structure With the schema of your test database instantiated, you need to populate it with test data. Create two test book records using YAML: test/fixtures/books.yml: lisp_cb: id: 1 title: 'Lisp Cookbook' isbn: '0596003137' java_cb: id: 2 title: 'Java Cookbook' isbn: '0596007019' Now add a test method named test_book_CRUD to the book unit test file. test/unit/book_test_crud.rb: require File.dirname(__FILE__) + '/../test_helper' class BookTest < Test::Unit::TestCase fixtures :books def test_book_CRUD lisp_cookbook = Book.new :title => books(:lisp_cb).title, :isbn => books(:lisp_cb).isbn assert lisp_cookbook.save lisp_book_copy = Book.find(lisp_cookbook.id) assert_equal lisp_cookbook.isbn, lisp_book_copy.isbn lisp_cookbook.isbn = "0596007973" assert lisp_cookbook.save assert lisp_cookbook.destroy end end Finally, run the test method: $ ruby ./test/unit/book_test_crud.rb DiscussionWhen the Book model was generated, ./script/generate model Book automatically created a series of test files. In fact, for every model you generate, Rails sets up a complete Test::Unit environment. The only remaining work for you is to define test methods and test fixtures. The BookTest class defined in book_test.rb inherits from Test::Unit::TestCase. This class, or test case, contains test methods. The test methods' names must begin with "test" for their code to be included when the tests are run. Each test method contains one or more assertions, which are the basic element of all tests in Rails. Assertions either pass or fail. The book records in the book.yml fixture file are labeled for easy referencing from within test methods. The solution defines two book records labeled lisp_cb and java_cb. When the data in this fixture file is included in a test case with fixtures :books, the methods of that class have access the fixture data via instance variable named after the labels of each record in the text fixture. The BookTest method starts off by creating a new Book object using the title and ISBN from the first record in the text fixture. The resulting object is stored in the lisp_cookbook instance variable. The first assertion tests that saving the Book object was successful. Next, the book object is retrieved using the find method and stored in another instance variable named lisp_book_copy. The success of this retrieval is tested in the next assertion, which compares the ISBNs of both book objects. At this point, we've tested the ability to create and read a database record. The solution tests updating by assigning a new ISBN to the object stored in lisp_cookbook and then asserts that saving the change is successful. Finally, the ability to destroy a Book object is tested. Here's the output of running the successful test case: $ ruby ./test/unit/book_test_crud.rb Loaded suite ./test/unit/book_test_crud Started . Finished in 0.05732 seconds. 1 tests, 4 assertions, 0 failures, 0 errors See Also
|