Home arrow Ruby-on-Rails arrow Page 2 - Adding Authors to an Online Bookstore
RUBY-ON-RAILS

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
TABLE OF CONTENTS:
  1. · Adding Authors to an Online Bookstore
  2. · Running Unit Tests
  3. · Creating the Controller
  4. · Implementing the User Stories

print this article
SEARCH DEVARTICLES

Adding Authors to an Online Bookstore - Running Unit Tests
(Page 2 of 4 )

Since ActiveRecord creates methods for each field in the database table for a given ActiveRecord model, Authorobjects now automatically respond to the methodsfirst_nameandlast_name. However, it would be nice to get the whole name of an author with a single method call. Letís first create a unit test that checks that our method works correctly.  

Opentest/unit/author_test.rband replace thetest_truthmethod with thetest_namemethod:

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

class AuthorTest < Test::Unit::TestCase
  fixtures :authors

  def test_name
    author = Author.create(:first_name => 'Joel',
                           :last_name => 'Spolsky')
    assert_equal 'Joel Spolsky', author.name
  end

end

Runningrake, we notice that the test will result in an error, because theAuthorclass doesnít have a method callednameyet. Letís fix that by openingapp/models/author.rband implementing the method.

class Author < ActiveRecord::Base
 
def name
    "#{first_name} #{last_name}"
  end

end

The method returns a string containing the return values of thefirst_nameandlast_namemethods separated by a space. Runningrakeagain tells us that everything is in order.

$ rake

--------------------------------------------
(in /home/george/projects/emporium)
/usr/local/bin/ruby -Ilib:test "/usr/local/lib/ruby/gems/1.8/gems/rake-0.7.1/lib/rake/rake_test_loader.rb" "test/unit/author_test.rb"
Loaded suite /usr/local/lib/ruby/gems/1.8/gems/rake-0.7.1/lib/rake/rake_test_loader
Started
.
Finished in 0.062438 seconds.

1 tests, 1 assertions, 0 failures, 0 errors
/usr/local/bin/ruby -Ilib:test "/usr/local/lib/ruby/gems/1.8/gems/rake-0.7.1/lib/rake/rake_test_loader.rb" "test/functional/about_controller_test.rb" Loaded suite /usr/local/lib/ruby/gems/1.8/gems/rake-0.7.1/lib/rake/rake_test_loader
Started
.
Finished in 0.021772 seconds.

1 tests, 1 assertions, 0 failures, 0 errors

/usr/local/bin/ruby -Ilib:test "/usr/local/lib/ruby/gems/1.8/gems/
rake-0.7.1/lib/rake/rake_test_loader.rb"
--------------------------------------------


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