Skip to content

Ruby class:Day 5

22/12/2010

This day i learned how to write test case for a program.
Test case validate a program.easily bugs fixed.
Program for concatenate tow string

class ‫Custormer
  def initialize(first_value,last_value)
    @first_value = first_value
    @second_value = last_value
   end
  def append_name_to_file(file)
  file << @first_value<<" "<< @second_value
  end
end

test case for the program

require 'test\unit'
require 'ducktype.rb'
class TestAddCustomer < Test::Unit::TestCase
  def test_add
    c=Customer.new("loge","san")
    f=File.open("tmpfile","w") do|f|
      c.append_name_to_file(f)
    end
    f=File.open("tmpfile") do |f|
      string=f.gets
      assert_equal("loge san",string)
      assert_equal("Chandrasekar babu",string)
    end
    #ensure
      #File.delete("tmpfile") if File.exist?("tmpfile")
  end

  def first_name_test
     c=Customer.new("loge","san")
     File.open("tmpfile") do |f|
       string = f.gets.split(' ').first
     end
     assert_equal("loge",string)
  end
end

==>   << -it used to concatenate the strings
==>   require – it import other rb file to current file use
==>    < Test::Unit::TestCase  – It derived test method from pre-defined class

Advertisements

From → Ruby

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: