2011-08-02 47 views
0

嗨,我正在学习编写测试的情况下,我开始小试的情况下为小programs.Iam试图程序测试用例错误?

require 'test/unit' 
class YearTest < Test::Unit::TestCase 
def test_hours(m) 
    p = Year.new 
    assert_equal(p.hours,m) 
    # assert_equal(p.hours,8784) 
end 
end 
class Year 
    def hours(a) 
    d = 24 
    if (a%400 == 0) 
    return h = d * 366 
    else 
    return h = d * 365 
    end 
    puts h 
    end 
end 
puts "enter a year" 
n = gets.to_i 
y = Year.new 
y.hours(n) 

,当我在NetBeans中运行这个程序我在测试用例得到错误..任何人都可以帮助解决这个问题吗?

回答

0

你的测试用例需要指定一个特定的情况;它并不打算像你正在做的那样交互式地完成。

像这样的事:

require 'test/unit' 

class YearTest < Test::Unit::TestCase 
    def test_hours 
    y = Year.new 
    assert_equal 8760, y.hours(2001) 
    assert_equal 8784, y.hours(1996) 
    end 
end 
+0

我在哪里可以了解写这个测试用例?你能告诉我杰勒米吗? –

+0

嗯。 Ruby上的Wikibook这部分看起来很合理。 http://en.wikibooks.org/wiki/Ruby_Programming/Unit_testing 这是一个旧视频,但我认为这仍然是如何使用Test :: Unit进行测试驱动开发的一个很好的演示。 http://screencasts.textmate.org/ruby_quiz_screencast.mov –