Now that we've built an online bookstore application in Ruby-on-Rails, it's time to serve our customers by building a book catalog of the store that they can use for browsing, viewing book details, searching for books, and finding out about new titles at a glance. This article, the first in a three-part series, is excerpted from chapter four of the book Practical Rails Projects, written by Eldon Alameda (Apress; ISBN: 1590597818).
Building an Online Book Catalog - Implementing the Book Catalog Interface (Page 2 of 4 )
To be able to really test browsing a list of titles, we need to have a number of books available for viewing. Therefore, we need to expand our authors.yml,publishers.yml,books.yml, andauthors_books.ymlfixture files intest/fixtures. You can download the files from the Source Code/Downloads section ofwww.apress.com.
As in the previous chapter, we’ll use integration tests for this sprint, because they work well to exercise the book catalog browsing system from end to end. First, we’ll create a test stub by using the Rails test generator:
def enter_site(name) open_session do |session| session.extend(BrowsingTestDSL) session.name = name yield session if block_given? end end end
In the test case, we create a DSL module, as in the previous chapter. We first test that requesting"/catalog"works and that we get the defaultindex template rendered. Then we check that there is a list of books (laid out with an HTML definition list) on the page and that a book called The Idiot exists in that list.
Our test fails magnificently, in plain old TDD way, so it’s time to implement the functionality.
First, we need to create the controller for the catalog. We’ll name itcatalogso that it will match the URL requested in thebrowse_sitetest method. Once again, use the familiarscript/generatecommand and give the needed action names (for all our projected user stories) as parameters.
$ script/generate controller Catalog index show search latest