Contents 

Ruby on Rails:
Table of Contents
Preface
Zero to Sixty: Introducing Rails
1.1. Rails Strengths
1.2. Putting Rails into Action
1.3. Organization
1.4. The Web Server
1.5. Creating a Controller
1.6. Building a View
1.7. Tying the Controller to the View
1.8. Under the Hood
1.9. What's Next?
Active Record Basics
2.1. Active Record Basics
2.2. Introducing Photo Share
2.3. Schema Migrations
2.4. Basic Active Record Classes
2.5. Attributes
2.6. Complex Classes
2.7. Behavior
2.8. Moving Forward
Active Record Relationships
3.1. belongs_to
3.2. has_many
3.3. has_one
3.4. What You Haven't Seen
3.5. Looking Ahead
Scaffolding
4.1. Using the Scaffold Method
4.2. Replacing Scaffolding
4.3. Generating Scaffolding Code
4.4. Moving Forward
Extending Views
5.1. The Big Picture
5.2. Seeing Real Photos
5.3. View Templates
5.4. Setting the Default Root
5.5. Stylesheets
5.6. Hierarchical Categories
5.7. Styling the Slideshows
Ajax
6.1. How Rails Implements Ajax
6.2. Playing a Slideshow
6.3. Using Drag-and-Drop to Reorder Slides
6.4. Drag and Drop Everything (Almost Everything)
6.5. Filtering by Category
Testing
7.1. Background
7.2. Ruby's Test::Unit
7.3. Testing in Rails
7.4. Wrapping Up
Installing Rails
1.1. Windows
2.1. OS X
3.1. Linux
Quick Reference
5.1. General
5.2. Testing
5.3. RJS (Ruby JavaScript)
5.4. Active Record
5.5. Controllers
5.6. Views
5.7. Ajax
5.8. Configuring Your Application
About the Authors
Colophon
Index
A
B
C
D
E
F
G
H
I
J
L
M
N
O
P
R
S
T
U
V
W
X
Y
Z

Ruby on Rails manual

Prev Page Next Page
Previous Page
Next Page

5.2. Testing

rake test                      # Test all units and functionals
rake test:functionals          # Run tests for functionals
rake test:integration          # Run tests for integration
rake test:units                # Run tests for units

5.2.1. Unit Tests

rake test:units

Available assertions:

assert_kind_of Class, @var  # same class
assert @var                 # not nil
assert_equal 1, @p.id       # equality
@product.destroy
assert_raise(ActiveRecord::RecordNotFound) { Product.find( @product.id ) }

5.2.2. Functional Tests

rake test:functionals

5.2.2.1. Requests
get :action # a get request of the specificed action
get :action, :id => 1,
         { session_hash }, # optional session variables
         { flash_hash }    # optional messages in the flash

post :action, :foo => { :value1 => 'abc', :value2 => '123' },
              { :user_id => 17 },
              { :message => 'success' }

get, post, put, delete, head

assert_response :success
# possible parameters are:
#   :success
#   :redirect
#   :missing
#   :error

5.2.2.2. Redirects
assert_redirected_to :action => :other_action
assert_redirected_to :controller => 'foo', :action => 'bar'
assert_redirected_to http://www.invisible.ch

5.2.2.3. Rendered with Template
assert_template "post/index"

5.2.2.4. Variable Assignments
assert_nil assigns(:some_variable)
assert_not_nil assigns(:some_variable)
assert_equal 17, assigns(:posts).size

5.2.2.5. Rendering of Specific Tags
assert_tag :tag => 'body'
assert_tag :content => 'Rails Seminar'
assert_tag :tag => 'div', :attributes => { :class => 'index_list' }
assert_tag :tag => 'head', :parent => { :tag => 'body' }
assert_tag :tag => 'html', :child => { :tag => 'head' }
assert_tag :tag => 'body', :descendant => { :tag => 'div' }
assert_tag :tag => 'ul',
           :children => { :count => 1..3,
                      :only => { :tag => 'li' } }

5.2.3. Integration Tests

rake test:integration

Hypothetical integration test:

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

class UserManagementTest < ActionController::IntegrationTest
  fixtures :users, :preferences
  def test_register_new_user
    get "/login"
    assert_response :success
    assert_template "login/index"

    get "/register"
    assert_response :success
    assert_template "register/index"

    post "/register",
         :user_name => "happyjoe",
         :password => "neversad"
    assert_response :redirect
    follow_redirect!
    assert_response :success
    assert_template "welcome"
end

Learn more: http://jamis.jamisbuck.org/articles/2006/03/09/integration-testing-in-rails-1-1.

5.2.4. More on Testing

Learn more: http://manuals.rubyonrails.com/read/site/5.

5.2.4.1. rake

rake is the Ruby version of a make utility. Rails defines a number of rake tasks:

rake db:fixtures:load          # Load fixtures into the current environment's
                               # database
                               # Load specific fixtures using FIXTURES=x,y
rake db:migrate                # Migrate the database through scripts in
                               # db/migrate. Target
                               # specific version with VERSION=x
rake db:schema:dump            # Create a db/schema.rb file that can be portably
                               # used against any DB supported by AR
rake db:schema:load            # Load a schema.rb file into the database
rake db:sessions:clear         # Clear the sessions table
rake db:sessions:create        # Creates a sessions table for use with
                               # CGI::Session::ActiveRecordStore
rake db:structure:dump         # Dump the database structure to a SQL file
rake db:test:clone             # Recreate the test database from the current
                               # environment's database schema
rake db:test:clone_structure   # Recreate the test databases from the development
                               # structure
rake db:test:prepare           # Prepare the test database and load the schema
rake db:test:purge             # Empty the test database

rake doc:app                   # Build the app HTML Files
rake doc:clobber_app           # Remove rdoc products
rake doc:clobber_plugins       # Remove plugin documentation
rake doc:clobber_rails         # Remove rdoc products
rake doc:plugins               # Generate documation for all installed plugins
rake doc:rails                 # Build the rails HTML Files
rake doc:reapp                 # Force a rebuild of the RDOC files
rake doc:rerails               # Force a rebuild of the RDOC files

rake log:clear                 # Truncates all *.log files in log/ to zero bytes

rake rails:freeze:edge         # Lock this application to latest Edge Rails. Lock a
                               # specific revision with REVISION=X
rake rails:freeze:gems         # Lock this application to the current gems (by
                               # unpacking them into vendor/rails)
rake rails:unfreeze            # Unlock this application from freeze of gems or
                               # edge and return to a fluid use of system gems

rake rails:update              # Update both scripts 
 
 and public/javascripts from
                               # Rails
rake rails:update:javascripts  # Update your javascripts from your current rails
                               # install
rake rails:update:scripts      # Add new scripts to the application script/
                               # directory

rake stats                     # Report code statistics (KLOCs, etc) from the
                               # application

rake test                      # Test all units and functionals
rake test:functionals          # Run tests for functionalsdb:test:prepare
rake test:integration          # Run tests for integrationdb:test:prepare
rake test:plugins              # Run tests for pluginsenvironment
rake test:recent               # Run tests for recentdb:test:prepare
rake test:uncommitted          # Run tests for uncommitteddb:test:prepare
rake test:units                # Run tests for unitsdb:test:prepare

rake tmp:cache:clear           # Clears all files and directories in tmp/cache
rake tmp:clear                 # Clear session, cache, and socket files from tmp/
rake tmp:create                # Creates tmp directories for sessions, cache, and
                               # sockets
rake tmp:sessions:clear        # Clears all files in tmp/sessions
rake tmp:sockets:clear         # Clears all ruby_sess.* files in tmp/sessions

5.2.5. Scripts

script/about            # Information about environenment
script/breakpointer     # starts the breakpoint server
script/console          # interactive Rails Console
script/destroy          # deletes files created by generators
script/generate         # -> generators
script/plugin           # -> Plugins
script/runner           # executes a task in the rails context
script/server           # launches the development server
                        # http://localhost:3000

script/performance/profiler     # profile an exspesive method
script/performance/benchmarker  # benchmark different methods

script/process/reaper
script/process/spawner

5.2.6. Generators

ruby script/generate model ModellName
ruby script/generate controller ListController show edit
ruby script/generate scaffold ModelName ControllerName
ruby script/generate migration AddNewTable
ruby script/generate plugin PluginName
ruby script/generate mailer Notification lost_password signup
ruby script/generate web_service ServiceName api_one api_two
ruby script/generate integration_test TestName
ruby script/generate session_migration

Options:



-p or --pretend

Run but do not make any changes.



-f or --force

Overwrite files that already exist.



-s or --skip

Skip files that already exist.



-q or --quiet

Suppress normal output.



-t or --backtrace

Debugging: show backtrace on errors.



-h or --help

Show this help message.



-c or --svn

Modify files with subversion (note: svn must be in path).

5.2.7. Plug-ins

script/plugin discover          # discover plugin repositories
script/plugin list              # list all available plugins
script/plugin install where     # install the "where" plugin
script/plugin install -x where  # install where plugin as SVN external
script/plugin install http://invisible.ch/projects/plugins/where
script/plugin update            # update installed plugins
script/plugin source            # add a source repository
script/plugin unsource          # removes a source repository
script/plugin sources           # lists source repositories

Learn more: http://wiki.rubyonrails.com/rails/pages/Plugins.

Searchable directory of plug-ins: agilewebdevelopment.com/plugins">http://www.agilewebdevelopment.com/plugins.


Previous Page
Next Page