Testing a Book Inventory Management System - Unit Testing Validations
(Page 2 of 2 )
You want to be absolutely sure that the validations are working. One way of doing this is to create a unit test that tests that all fields are validated correctly.
Scaffolding already created a unit test for you, but it contains only a dummy test, so replace the code intest/unit/book_test.rbwith the following code:
class BookTest < Test::Unit::TestCase def test_failing_create book = Book.new assert_equal false, book.save
assert_equal 7, book.errors.size assert book.errors.on(:title) assert book.errors.on(:publisher) assert book.errors.on(:authors) assert book.errors.on(:published_at) assert book.errors.on(:isbn) assert book.errors.on(:page_count) assert book.errors.on(:price) end end
The unit test creates a new book without specifying any values for any of the validated fields, such as the price. It then tries to save the book to the database. This triggers a validation of each of the fields to which you added validations. When you run the test, there is a validation failure on each of the fields—eight in total. We test for this condition with theassert_equalmethod. We also check that validations are done on the correct fields, by callingbook.errors.onon each validated field. This method returns the actual error message for the specified field, and if it returns nil, we know the validation is not working.
Run the unit test, and you should see it pass without errors:
$ ruby test/unit/book_test.rb
-------------------------------------------- Loaded suite test/unit/book_test Started . Finished in 0.172 seconds.
You should also test that a valid book can be saved to the database, by adding thetest_createmethod to the unit test, as follows:
fixtures :authors, :publishers
def test_create book = Book.new( :title => 'Ruby for Toddlers', :publisher_id => Publisher.find(1).id, :published_at => Time.now, :authors => Author.find(:all), :isbn => '123-123-123-1', :blurb => 'The best book since "Bodo Bär zu Hause"', :page_count => 12, :price => 40.4 )
assert book.save end
This test looks up an author and a publisher from the database. These are inserted by the publisher and author fixtures, which have also been added. The test creates a new book with valid parameters, including a publisher and author, and then validates that the book was saved successfully. Recall that callingsaveon the book object returnstrueif there are no validation errors.
Please check back next week for the continuation of this series.
DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.