Section 5.2. Testing


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/book/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: http://www.agilewebdevelopment.com/plugins.



Ruby on Rails[c] Up and Running
Ruby on Rails[c] Up and Running
ISBN: B003D3OGCY
EAN: N/A
Year: 2006
Pages: 94

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net