Home arrow Ruby-on-Rails arrow Page 4 - Adding Authors to an Online Bookstore

Adding Authors to an Online Bookstore

In this second part of a four-part article series on creating an online bookstore application with Ruby on Rails, you'll learn how to run a migration script and unit tests, create the controller, and more. This article is excerpted from chapter two 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 12, 2010
  1. · Adding Authors to an Online Bookstore
  2. · Running Unit Tests
  3. · Creating the Controller
  4. · Implementing the User Stories

print this article

Adding Authors to an Online Bookstore - Implementing the User Stories
(Page 4 of 4 )

“Test-driven development sure must be great,” says George, “but I want to see something real, plenty soon.” He is also not happy to hear that the sprints normally take more than a week to implement. We reassure him that using Rails, the sprints really live up to their names, and that we should have the first sprint ready before he shuts his doors for the day. A bold promise, so let’s move on.

As you saw in our sprint backlog in Chapter 1 (Table 1-2), we need to implement five user stories in this sprint:

  1. Add author: When George hears about a new cool author, he must be able to get that author on his site. A click, typing the author details in a form, submitting the form, and the info should be there.
  2. List authors: The author administration interface needs a homepage, which also works as a list of all authors. 
  3. View author: When the system evolves and more information about authors is added, there must be pages showing the details of an individual author. 
  4. Edit author: Sometimes, author details change. Some people get married; others convert to a cargo cult and change their name to Ilyushin. Either way, nothing is perpetual. Changing author details needs to be as easy for George as adding new authors. 
  5. Delete author: During his vacation, George’s summer aides sometimes add totally bogus stuff into the system. They think it’s funny. George doesn’t, and neither do many of his customers. So it’s important that authors can be deleted from the system, too.

We will proceed story by story, first writing a functional test for the story and then making the test pass by implementing the feature.

Adding an Author

The first user story to implement in this sprint is adding a new author.

Adding a Test Case

First, we just want to request the page with the form for adding the author and make sure that we’re handed the correct template. We add a test case for it intest/functional/admin/ author_controller_test.rb, which is the functional test file for our controller. When we open the file, we can see that there is a dummytest_truthtest method in the file. As the comment above the method suggests, we remove it and replace it with our own test,test_new, shown in Listing 2-1.

Listing 2-1. test/functional/admin/author_controller_test.rb

require File.dirname(__FILE__) + '/../../test_helper'
require 'admin/author_controller'

# Re-raise errors caught by the controller.
class Admin::AuthorController; def rescue_action(e) raise e end; end

class Admin::AuthorControllerTest < Test::Unit::TestCase
def setup
@controller = Admin::AuthorController.new
@request    = ActionController::TestRequest.new
@response   = ActionController::TestResponse.new

  def test_new
get :new
assert_template 'admin/author/new'
assert_tag 'P', :content => 'Create new author'
assert_tag 'form', :attributes => {:action => '/admin/author/create'}


In the test, we first use theget test helper method to call thenewaction. Then we use the Rails-specificassert_templatemethod to test that we were indeed rendered the correct view file. Last, we check that the rendered template indeed looks as it should. We do this by testing that there are two HTML elements present: a level 1 heading with the content “Create new author,” and a form element pointing to/admin/author/create.

Now we can go back to the command line and run our test case. We don’t want to execute all the tests this time, so we just run the test file we’re working on right now.

$ ruby test/functional/admin/author_controller_test.rb

Loaded suite test/functional/admin/author_controller_test Started
Finished in 0.052617 seconds.

  1) Failure:
test_new(Admin::AuthorControllerTest) [test/functional/admin/author_controller_test.rb:17]: expected tag, but no tag found matching {:content=>
'Create new author', :tag=>'h1'} in:
"<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<html xmlns=\http://www.w3.org/1999/xhtml\>
  <head>\n   <title>Create new author</title>
    <link href="/stylesheets/style.css?1149578791" media="screen"

rel="Stylesheet" type="text/css" />

<nil> is not true.

1 tests, 2 assertions, 1 failures, 0 errors

We get a failure, because there wasn’t a level 1 heading on the returned page. This was to be expected, since we haven’t implemented the page yet.

Note  Notice that there was only one failure, even though the page doesn’t contain a form element either. This is because a test method is aborted upon the first failure.

Please check back tomorrow for the next part 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.

blog comments powered by Disqus

- 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 

Developer Shed Affiliates


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