Home arrow Ruby-on-Rails arrow Page 4 - Browsing and Searching an Online Book Catalog
RUBY-ON-RAILS

Browsing and Searching an Online Book Catalog


In this second part of a three-part series on building an online book catalog in Ruby-on-Rails, you'll learn how to set up the application to allow users to view book details and search for books. This article is excerpted from chapter four of the book Practical Rails Projects, written by Eldon Alameda (Apress; ISBN: 1590597818).

Author Info:
By: Apress Publishing
Rating: 5 stars5 stars5 stars5 stars5 stars / 1
May 18, 2010
TABLE OF CONTENTS:
  1. · Browsing and Searching an Online Book Catalog
  2. · Adding Links
  3. · Implementing the Search Books User Story
  4. · Updating the Integration Test

print this article
SEARCH DEVARTICLES

Browsing and Searching an Online Book Catalog - Updating the Integration Test
(Page 4 of 4 )

We start by extending our integration test to span searching, too. We do this by adding a new method,searches_for_tolstoy, to theBrowsingTestDSLmodule intest/integration/ browsing_and_searching_test.rb, as shown in Listing 4-2.

Listing 4-2. Test Method for Book Searches

require "#{File.dirname(__FILE__)}/../test_helper"

class BrowsingAndSearchingTest < ActionController::IntegrationTest
  fixtures :publishers, :authors, :books, :authors_books

  def test_browsing_the_site
    jill = enter_site(:jill)
    jill.browse_index
    jill.go_to_second_page

    jill.searches_for_tolstoy
 
end

  def test_getting_details
    jill = enter_site(:jill)
    jill.get_book_details_for "Pride and Prejudice"
  end

  private

  module BrowsingTestDSL
   
include ERB::Util
    attr_writer :name

    def browse_index
      get "/catalog"

      assert_response :success
      assert_template "catalog/index"
      assert_tag      :tag => "dl", :attributes =>
                        { :id => "books" },
                      :children =>
                        { :count => 10, :only =>
                          {:tag => "dt"}}
      assert_tag :tag => "dt", :content => "The Idiot"
      check_book_links
   
end

    def go_to_second_page
      get "/catalog?page=2"
      assert_response :success
      assert_template "catalog/index"
      assert_equal    Book.find_by_title("Pro Rails E-Commerce"),
                      assigns(:books).last
      check_book_links
    end

    def get_book_details_for(title)
      @book = Book.find_by_title(title)

      get /catalog/show/#{@book.id}
      assert_response :success
      assert_template "catalog/show"

      assert_tag      :tag => "h1",
                      :content => @book.title
      assert_tag      :tag => "h2",
                      :content => "by #{@book.authors.map{|a| a.name}}"
    end

    def searches_for_tolstoy
      leo = Author.find_by_first_name_and_last_name("Leo", "Tolstoy")

      get "/catalog/search?q=#{url_encode("Leo Tolstoy")}"
      assert_response :success
      assert_template "catalog/search"

      assert_tag      :tag => "dl", :attributes =>
                        { :id => "books" },
                      :children =>
                        { :count => leo.books.size, :only =>
                          {:tag => "dt"}}

      leo.books.each do |book|
        assert_tag :tag => "dt", :content => book.title
      end
    end

    def check_book_links
      for book in assigns(:books)
        assert_tag    :tag => "a", :attributes =>
                        { :href => "/catalog/show/#{book.id}"}
      end
    end
  end

  def enter_site(name)
   
open_session do |session|
     
session.extend(BrowsingTestDSL)
      session.name = name
     
yield session if block_given?
   
end
  end
end

Our new test method makes Jill search for Leo Tolstoy with a search form and checks that the resulting result list will have exactly as many books as Leo has provided the shop, namely two. We use theurl_encodemethod to escape white space from the search string. It is provided by theERB::Utillibrary, so we need to require it at the beginning of our module. Last, we test that the books in the resulting list have the correct names by going through all the books written by Leo and checking that there is adtelement containing the book’s title.

Please check back tomorrow for the conclusion to 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.

blog comments powered by Disqus
RUBY-ON-RAILS ARTICLES

- Ruby-on-Rails Faces Second Security Flaw in ...
- Ruby 2.0 Prepped for February 2013 Release
- Why LinkedIn Switched from Ruby on Rails
- Adding Style with Action Pack
- Handling HTML in Templates with Action Pack
- Filters, Controllers and Helpers in Action P...
- Action Pack and Controller Filters
- Action Pack Categories and Events
- Logging Out, Events and Templates with Actio...
- Action Pack Sessions and Architecture
- More on Action Pack Partial Templates
- Action Pack Partial Templates
- Displaying Error Messages with the Action Pa...
- Action Pack Request Parameters
- Creating an Action Pack Registration Form

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials