classEmployee
definitialize(name,salary,hire_year)
@name=name
@salary=salary
@hire_year=hire_year
end
attr_reader:name,:salary,:hire_year
defraise_salary_by(perc)
@salary+=(@salary*(perc*0.01))
end
defto_s
"Nameis#{@name},salaryis#{@salary},"+"hireyearis#{@hire_year}"
end
end
响应的单位测试以下:
列表2.响应于Employee类的单位测试
requiretest/unit/testcase
requiretest/unit/autorunner
requirehr
classTestEmployee<Test::Unit::TestCase
@@Test_Salary=2500
defsetup
@emp=Employee.new("Homer",@@Test_Salary,2003)
end
deftest_raise_salary
@emp.raise_salary_by(10)
expected=(@@Test_Salary*0.10)+@@Test_Salary
assert(expected==@emp.salary)
end
end
为了运转这个单位测试,你能够为该单位测试类创立一个Run设置-作为一个Test::Unit范例,见2。