Database Articles

  Home arrow Database Articles arrow Integration Testing for a Book Invento...

Integration Testing for a Book Inventory Management System
By: Apress Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating:  stars stars stars stars stars / 0

    Table of Contents:
  • Integration Testing for a Book Inventory Management System
  • Changing the Controller



    Integration Testing for a Book Inventory Management System

    (Page 1 of 2 )

    In this ninth part of a multi-part series on the scaffolding feature of Ruby on Rails, we'll finish the add book user story, and start creating an integration test for our book inventory management system. This article is excerpted from chapter three of the book Practical Rails Projects, written by Eldon Alameda (Apress; ISBN: 1590597818).

    Completing the Add Book User Story 

    As you have noticed, throughout this chapter, we haven’t followed TDD very strictly. Instead, we first created the code using scaffolding. Although we can add, list, view, edit, and delete books, the functionality is not tested and we are not confident that it is working as George desires. We’ll have to talk to George to find out what exactly should be implemented.

    We call George over to our cubicle, which happens to be the only cubicle in the office, reserved exclusively for consultants. George tells us that when adding a book to the system, he must be able to enter all details of the book, including title, price, ISBN, blurb, and so on. Furthermore, George tells us that the current system is difficult to use. Because of this, he has to consult his computer-literate nephew, who enters the details of new books into the system. The blurb text is what is causing him most troubles. When displayed on the website, the blurb text must be nicely styled with, for example, proper headings, bulleted lists, and italicized text, so that it looks as good as possible. On the Web, this requires HTML skills, and because George doesn’t know HTML, he can’t write the blurb himself. Luckily, there’s a simple answer to the problem called Textile.

    Textile is a simple text markup language that can be used to write content for the Web without needing to know HTML. RedCloth is a Ruby module that adds Textile support to Rails applications. You’ll see how this works when we implement the View Book user story, later in the chapter.

    Our first task is to create the integration test we will use to test the book administration implementation.

    Creating an Integration Test

    We’ll create a DSL that will closely match the actions performed in the book administration user stories. Create the integration test by executing the following command:

    $ script/generate integration_test book

    exists test/integration/
    create test/integration/book_test.rb

    As with unit tests, the integration test contains only a dummy test, so modify thetest/integration/book_test.rbfile as shown in Listing 3-2.


    A block is a piece of Ruby code that can be passed to a Ruby method. Unlike normal parameters, blocks can be passed to all Ruby methods without explicitly declaring that the method takes a block as a parameter. The method receiving the block, as a parameter, can evaluate the code, by calling the yield method.

    The following example shows how Ruby blocks can be used for preprocessing and postprocessing by passing a block to the log method.

    def log
    puts "before"
    puts "after"

    log { puts "in between" } # block on one line

    The following is the output of executing this example:

    in between

    The following syntax is preferred for blocks that span more than one line:

    log do

    Listing 3-2. First Version of Integration Test for the Book Administration Interface

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

    class BookTest < ActionController::IntegrationTest
    fixtures :publishers, :authors
    def test_book_administration
    publisher = Publisher.create(:name => 'Books for Dummies')
    author = Author.create(:first_name => 'Bodo', :last_name => 'Bär')

    george = new_session_as(:george)
    ruby_for_dummies = george.add_book :book => {
    :title => 'Ruby for Dummies',
    :publisher_id =>,
    :author_ids => [],
    :published_at =>,
    :isbn => '123-123-123-X',
    :blurb => 'The best book released since "Eating for Dummies"',
    :page_count => 123,
    :price => 40.4


    module BookTestDSL
    attr_writer :name

    def add_book(parameters)
    post "/admin/book/create", parameters
    assert_response :redirect
    assert_response :success
    assert_template "admin/book/list"
    assert_tag :tag => 'td', :content => parameters[:book][:title]
    return Book.find_by_title(parameters[:book][:title])

    def new_session_as(name)
    open_session do |session|
    session.extend(BookTestDSL) = name
    yield session if block_given?


    Note that thetest_book_administrationtest will be used for verifying that the whole book administration works from end to end. The first step in doing this is implementing a test for the Add Book user story.

    Also note that the methodnew_session_as(name)is used to open a new session for a virtual user. Inside the method, we use some Ruby magic to extend the newsessionobject at runtime with our book-testing DSL. This is done with theextendmethod, which simply adds the instance methods in theBookTestDSLmodule to thesessionobject.

    We also save the name of the user in an instance variable inside the DSL module. This allows you to use it later, if required.

    The lineyield session if block_given?is used to pass the new session to a block, if a block has been specified.

    The integration test performs the following actions, which verify that the Add Book user story works:

    1. Create a new author and publisher.
    2. Open a new session as George.
    3. Create a new book by calling thecreateaction with valid parameters.
    4. Verify that there is a redirection to the list books view, which should happen if the book was created successfully.

    Run the integration test, and you should see that all tests pass:

    $ ruby test/integration/book_test.rb

    Loaded suite test/integration/book_test Started
    Finished in 0.453 seconds.

    1 tests, 4 assertions, 0 failures, 0 errors

    Since the test didn’t fail, you could be tricked into believing that we have just finished the implementation of the Add Book user story, but you can see that this is not the case by opening a browser and going tohttp://localhost:3000/admin/book/new. You should see the front-end for the Add Book user story, as shown in Figure 3-6.

    The page you see on the screen was created by the scaffolding script, and includes drop-down lists for thepublished_at,created_at, andupdated_atfields. The values forcreated_atandupdated_atare generated by Rails automatically, so George shouldn’t have to see them. There’s also no way of specifying the authors or publisher of the book.

    Figure 3-6. Testing the Add Book user story

    More Database Articles Articles
    More By Apress Publishing

    blog comments powered by Disqus


    - MariaDB, a MySQL Alternative, Opens Foundati...
    - Oracle Supporting HP Itanium Again, Finally
    - Completing a Book Inventory Management System
    - Uploading Images for a Book Inventory Manage...
    - Finishing the Add Book Story for a Book Inve...
    - Integration Testing for a Book Inventory Man...
    - User Stories for a Book Inventory Management...
    - Unit Testing a Book Inventory Management Sys...
    - Testing a Book Inventory Management System
    - Implementing Models for a Book Inventory Man...
    - Book Inventory Application: Publishers and B...
    - Handling Publishers in a Book Inventory Mana...
    - Publisher Administration for Book Inventory ...
    - Book Inventory Management
    - Using the SQL Reference Manual

    Developer Shed Affiliates


    © 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap