Test Driven  Development  By  Uma Mahesh Varma
Test Drive Development  Introduction Importance of T.T.D Rails webs application directory structure  Explanation about test folder in rails directory structure  Unit Tests Functional Tests
What is meant by test drive development ?  The answer for the above question is as fallows, TTD (Test Drive Development) is an part of software development techniques based on writing some small test cases before implementing code or new functionalities.   Test drive development is nothing but developing a product by writing unit tests that define the code requirement. These tests returns true or false. Running these tests rapidly brings you a correct behavior of the developer to develop an application. 
Procedure of T.D.D Write a test.   Run the test and check whether it fails or not.  If not Write some code.  Run the automated test. Refactor.
Rails webs application directory structure my_app/ README Installation and usage information Rakefile Build script app/ Model, View, and Controller files go here components/ Reusable components config/ Configuration and database connection parameters db/ Schema and migration information doc/ Autogenerated documentation lib/ Shared code log/ Logfiles produced by your application public/ Web-accessible directory. Your application runs from here script/ Utility scripts test/ Unit, functional, and integration tests, fixtures, and mocks tmp/ Runtime temporary files vendor/ Imported code
Test directory  Fixtures  Integration Functional Unit test_helper.rb
Unit Test require File.dirname(__FILE__) + '/../test_helper‘ class UserTest < Test::Unit::TestCase fixtures :users def test_truth assert true end end
Unit Test >> ruby test/unit/test_user.rb Loaded suite test/unit/user_test Started EE Finished in 0.559942 seconds. 1) Error: test_truth(UserTest): MysqlError: Unknown database ‘myapp_test' 1 tests, 0 assertions, 0 failures, 2 errors
Unit Test A Database Just for Tests Myapp> mysqladmin -u root create myapp_test Myapp> ruby test/unit/myapp_test.rb Loaded suite test/unit/myapp_test Started E Finished in 0.06429 seconds. 1) Error: test_truth(ProductTest): ActiveRecord::StatementInvalid: MysqlError: Table ‘myapp_test. users' doesn't exist: DELETE FROM users 1 tests, 0 assertions, 0 failures, 1 errors
Unit Test  myapp > rake db:test:prepare Myapp > ruby test/unit/user_test.rb Loaded suite test/unit/user_test Started . Finished in 0.085795 seconds. 1 tests, 1 assertions, 0 failures, 0 errors
Unit Test A Real Unit Test validates_presence_of :name, :email, :mobilenum validates_numericality_of : mobilenum validates_uniqueness_of :name validates_format_of :image_url, :with => %r{\.(gif|jpg|png)$}i, :message => &quot;must be a URL for a GIF, JPG, or PNG image&quot;
Unit Test def test_invalid_with_empty_attributes user = User.new assert ! user .valid? assert user .errors.invalid?(:name) assert user .errors.invalid?(:email) assert user .errors.invalid?(:mobileno) assert user.errors.invalid?(:image_url) end
Unit Test  Myapp > ruby test/unit/user_test.rb Loaded suite test/unit/user_test Started .. Finished in 0.092314 seconds. 2 tests, 6 assertions, 0 failures, 0 errors
Unit Test def test_image_url ok = %w{ fred.gif fred.jpg fred.png FRED.JPG FRED.Jpg http://a.b.c/x/y/z/fred.gif } bad = %w{ fred.doc fred.gif/more fred.gif.more } ok.each do |name| user = User.new(:name => “mahesh&quot; , :email => “umamahesh_nyros@yahoo.com&quot; , :mobilenum => 9866439593, :image_url => name) assert user.valid?, usererrors.full_messages end bad.each do |name| user = User.new(:name => “mahesh&quot; , :email => “umamahesh_nyros@yahoo.com&quot; , :mobilenum => 9866439593, :image_url => name) assert user.valid?, usererrors.full_messages  end
Fixtures mahesh: id: 1 name: mahesh email:  [email_address] mobilenum: 9866439593 image_url: maheshprofile.jpg
Unit Test Fixtures :users def test_unique_name user = User.new(:name => user(:mahesh).name, :email => “umamahesh_nyros@yahoo.com&quot; , :mobilenum => 9866439593, :image_url => &quot;fred.gif&quot; ) assert !user.save assert_equal &quot;has already been taken&quot; , user.errors.on(:name) end
Unit test supports  assert(boolean,message) assert_equal(expected, actual,message) assert_not_equal(expected, actual,message) assert_nil(object,message) assert_not_nil(object,message) assert_match(pattern, string,message) assert_no_match(pattern, string,message)
Functional Testing of Controllers require ‘user_helper’ def UserTest < Test::Unit::Testclass def test_index assert true end end
Functional Testing of Controllers myapp > ruby test/functional/user_test_controller.rb Loaded suite test/functional/ user_test_controller.rb Started . Finished in 0.0604571 seconds. 1 tests, 1 assertions, 0 failures, 0 errors
Thank You

Test Drive Development in Ruby On Rails

  • 1.
    Test Driven Development By Uma Mahesh Varma
  • 2.
    Test Drive Development Introduction Importance of T.T.D Rails webs application directory structure Explanation about test folder in rails directory structure Unit Tests Functional Tests
  • 3.
    What is meantby test drive development ?  The answer for the above question is as fallows, TTD (Test Drive Development) is an part of software development techniques based on writing some small test cases before implementing code or new functionalities.  Test drive development is nothing but developing a product by writing unit tests that define the code requirement. These tests returns true or false. Running these tests rapidly brings you a correct behavior of the developer to develop an application. 
  • 4.
    Procedure of T.D.DWrite a test.  Run the test and check whether it fails or not. If not Write some code. Run the automated test. Refactor.
  • 5.
    Rails webs applicationdirectory structure my_app/ README Installation and usage information Rakefile Build script app/ Model, View, and Controller files go here components/ Reusable components config/ Configuration and database connection parameters db/ Schema and migration information doc/ Autogenerated documentation lib/ Shared code log/ Logfiles produced by your application public/ Web-accessible directory. Your application runs from here script/ Utility scripts test/ Unit, functional, and integration tests, fixtures, and mocks tmp/ Runtime temporary files vendor/ Imported code
  • 6.
    Test directory Fixtures Integration Functional Unit test_helper.rb
  • 7.
    Unit Test requireFile.dirname(__FILE__) + '/../test_helper‘ class UserTest < Test::Unit::TestCase fixtures :users def test_truth assert true end end
  • 8.
    Unit Test >>ruby test/unit/test_user.rb Loaded suite test/unit/user_test Started EE Finished in 0.559942 seconds. 1) Error: test_truth(UserTest): MysqlError: Unknown database ‘myapp_test' 1 tests, 0 assertions, 0 failures, 2 errors
  • 9.
    Unit Test ADatabase Just for Tests Myapp> mysqladmin -u root create myapp_test Myapp> ruby test/unit/myapp_test.rb Loaded suite test/unit/myapp_test Started E Finished in 0.06429 seconds. 1) Error: test_truth(ProductTest): ActiveRecord::StatementInvalid: MysqlError: Table ‘myapp_test. users' doesn't exist: DELETE FROM users 1 tests, 0 assertions, 0 failures, 1 errors
  • 10.
    Unit Test myapp > rake db:test:prepare Myapp > ruby test/unit/user_test.rb Loaded suite test/unit/user_test Started . Finished in 0.085795 seconds. 1 tests, 1 assertions, 0 failures, 0 errors
  • 11.
    Unit Test AReal Unit Test validates_presence_of :name, :email, :mobilenum validates_numericality_of : mobilenum validates_uniqueness_of :name validates_format_of :image_url, :with => %r{\.(gif|jpg|png)$}i, :message => &quot;must be a URL for a GIF, JPG, or PNG image&quot;
  • 12.
    Unit Test deftest_invalid_with_empty_attributes user = User.new assert ! user .valid? assert user .errors.invalid?(:name) assert user .errors.invalid?(:email) assert user .errors.invalid?(:mobileno) assert user.errors.invalid?(:image_url) end
  • 13.
    Unit Test Myapp > ruby test/unit/user_test.rb Loaded suite test/unit/user_test Started .. Finished in 0.092314 seconds. 2 tests, 6 assertions, 0 failures, 0 errors
  • 14.
    Unit Test deftest_image_url ok = %w{ fred.gif fred.jpg fred.png FRED.JPG FRED.Jpg http://a.b.c/x/y/z/fred.gif } bad = %w{ fred.doc fred.gif/more fred.gif.more } ok.each do |name| user = User.new(:name => “mahesh&quot; , :email => “umamahesh_nyros@yahoo.com&quot; , :mobilenum => 9866439593, :image_url => name) assert user.valid?, usererrors.full_messages end bad.each do |name| user = User.new(:name => “mahesh&quot; , :email => “umamahesh_nyros@yahoo.com&quot; , :mobilenum => 9866439593, :image_url => name) assert user.valid?, usererrors.full_messages end
  • 15.
    Fixtures mahesh: id:1 name: mahesh email: [email_address] mobilenum: 9866439593 image_url: maheshprofile.jpg
  • 16.
    Unit Test Fixtures:users def test_unique_name user = User.new(:name => user(:mahesh).name, :email => “umamahesh_nyros@yahoo.com&quot; , :mobilenum => 9866439593, :image_url => &quot;fred.gif&quot; ) assert !user.save assert_equal &quot;has already been taken&quot; , user.errors.on(:name) end
  • 17.
    Unit test supports assert(boolean,message) assert_equal(expected, actual,message) assert_not_equal(expected, actual,message) assert_nil(object,message) assert_not_nil(object,message) assert_match(pattern, string,message) assert_no_match(pattern, string,message)
  • 18.
    Functional Testing ofControllers require ‘user_helper’ def UserTest < Test::Unit::Testclass def test_index assert true end end
  • 19.
    Functional Testing ofControllers myapp > ruby test/functional/user_test_controller.rb Loaded suite test/functional/ user_test_controller.rb Started . Finished in 0.0604571 seconds. 1 tests, 1 assertions, 0 failures, 0 errors
  • 20.