CSC/ECE 517 Fall 2011/ch2 2e kt: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
No edit summary
Line 1: Line 1:
== Ruby Testing Frameworks ==
== Ruby Testing Frameworks ==
== Demo Code ==
== Demo Code ==
Line 24: Line 23:
       @balance -= amount
       @balance -= amount
     end
     end
  end
== Test::Unit ==
  require "test/unit"
  require_relative("../Account.rb")
  class AccountTest < Test::Unit::TestCase
    def setup
    end
   
    def teardown
    end
   
    def test_balance
      a = Account.new(100)
      assert_equal(100, a.balance())
    end
 
    def test_deposit
      a = Account.new(100)
      assert_equal(200, a.deposit(100))
    end
   
    def test_withdrawal
      a = Account.new(100)
      assert_equal(50, a.withdrawal(50))
    end
   
    def test_name
      a = Account.new(100)
      a.name = "Checking"
      assert_not_nil(a.name())
    end
 
    def test_interest
      a = Account.new(100)
      assert_equal(a.addinterest(0.5), 150)
    end
 
    def test_fail
      a = Account.new(100)
      assert_equal(a.balance(), 200)
    end
 
   end
   end

Revision as of 15:44, 17 September 2011

Ruby Testing Frameworks

Demo Code

 class Account
   @balance
   @name
 
   attr_accessor :balance
   attr_accessor :name
 
   def initialize(amount)
     @balance = amount
   end
 
   def deposit(amount)
     @balance += amount
   end
 
   def addinterest(rate)
     @balance *= (1 + rate)
   end
 
   def withdrawal(amount)
     @balance -= amount
   end
 end


Test::Unit

 require "test/unit"
 require_relative("../Account.rb")
 class AccountTest < Test::Unit::TestCase
   def setup
   end
   
   def teardown
   end
   
   def test_balance
     a = Account.new(100)
     assert_equal(100, a.balance())
   end
 
   def test_deposit 
     a = Account.new(100)
     assert_equal(200, a.deposit(100))
   end
   
   def test_withdrawal
     a = Account.new(100)
     assert_equal(50, a.withdrawal(50))
   end
   
   def test_name
     a = Account.new(100)
     a.name = "Checking"
     assert_not_nil(a.name())
   end
 
   def test_interest
     a = Account.new(100)
     assert_equal(a.addinterest(0.5), 150)
   end
 
   def test_fail
     a = Account.new(100)
     assert_equal(a.balance(), 200)
   end
 
 end