2012-10-23 62 views
0

我想用測試單元做一個簡單的測試用例,在Ruby 1.9.1(Ubuntu Linux 12.04 64位)中:但它說我缺少TestCase的初始化方法參數。當我看到錯誤時,它說我試圖啓動/usr/lib/ruby/1.9.1/minitest。無法定義一個簡單的單元測試測試用例

但是,我之前安裝了測試單元gem。

這是第一個源的文件: Run.rb

#!/usr/bin/env ruby 

require "test/unit" 
require_relative "NumbersSet.rb" 

def draw_result 
    the_drawn_result = nil 
    loop do 
     the_drawn_result = rand(1000) 
     break if the_drawn_result >= 100 
    end 
    the_drawn_result 
end 

def do_operation(operation, number_1, number_2) 
    return nil if number_1 < 1 or number_2 < 1 
    number_1, number_2 = number_2, number_1 if number_1 < number_2 
    case operation 
     when :add 
      result = number_1 + number_2 
      return "#{number_1} + #{number_2} = #{result}", result 
     when :sub 
      return nil if number_1 == number_2 # A zero result will not help us 
      result = number_1 - number_2 
      return "#{number_1} - #{number_2} = #{result}", result 
     when :mul 
      return nil if number_1 == 1 or number_2 == 1 # Otherwise, this would be identity operation 
      result = number_1 * number_2 
      return "#{number_1} * #{number_2} = #{result}", result 
     when :div 
      return nil if number_1 == 1 or number_2 == 1 # Otherwise, this could be identity operation 
      return nil if number_1 % number_2 != 0 
      result = number_1/number_2 
      return "#{number_1}/#{number_2} = #{result}", result 
     else 
      raise "Unknown operation #{operation} !" 
    end 
end 

def play 
    drawn_numbers = NumbersSet.draw 
    puts drawn_numbers.inspect, draw_result 
end 

class DrawTest < Test::Unit::TestCase 
    def draw_test 

     assert_equal(nil, do_operation(:add, -3, 1)) 
     assert_equal(nil, do_operation(:add, 1, -1)) 
     assert_equal(nil, do_operation(:add, 5, 0)) 
     assert_equal(nil, do_operation(:add, 0, 5)) 
     assert_equal(nil, do_operation(:add, 3, 5)) 
     assert_equal(nil, do_operation(:add, 5, 3)) 

     assert_equal(nil, do_operation(:sub, 5, 3)) 
     assert_equal(nil, do_operation(:sub, 3, 5)) 
     assert_equal(nil, do_operation(:sub, 3, 3)) 

     assert_equal(nil, do_operation(:mul, 3, 5)) 
     assert_equal(nil, do_operation(:mul, 5, 3)) 
     assert_equal(nil, do_operation(:mul, 3, 1)) 
     assert_equal(nil, do_operation(:mul, 1, 3)) 

     assert_equal(nil, do_operation(:div, 20, 3)) 
     assert_equal(nil, do_operation(:div, 3, 20)) 
     assert_equal(nil, do_operation(:div, 20, 1)) 
     assert_equal(nil, do_operation(:div, 1, 20)) 
     assert_equal(nil, do_operation(:div, 20, 4)) 
     assert_equal(nil, do_operation(:div, 5, 20)) 

    end 
end 

OPERATIONS = [:add, :sub, :mul, :div] 

DrawTest.new.draw_test 

這是第二個文件:NumbersSet.rb

class NumbersSet 

    def self.draw 
     plaques = {} 
     (1..10).each { |value| plaques[value] = 2} 
     [25,50,75,100].each { |value| plaques[value] = 1} 

     draw_possibilities = (1..10).to_a << 25 << 50 << 75 << 100 
     the_draw = [] 
     6.times do |loop_index| 
      drawn_number_index = nil 
      loop do 
       drawn_number_index = rand(draw_possibilities.size) 
       break if plaques[draw_possibilities[drawn_number_index]] > 0 
      end 
      the_draw << draw_possibilities[drawn_number_index] 
      plaques[draw_possibilities[drawn_number_index]] -= 1 
     end 
     the_draw 
    end 

end 

這此終端錯誤:

$ ./Run.rb 
/usr/lib/ruby/1.9.1/minitest/unit.rb:971:in `initialize': wrong number of arguments (0 for 1) (ArgumentError) 
    from ./Run.rb:77:in `new' 
    from ./Run.rb:77:in `<main>' 

那麼,這真的是我自己編寫的一個糟糕的代碼,或者是一個安裝問題? 在此先感謝。

回答

0

我發現了什麼不好:

在文件Run.rb,在那裏我定義我的TestCase的子類:

  1. 的DrawTest類,如測試::單位的子類:: TestCase的必須定義其測試方法的名稱以test =>開頭,所以不是draw_test。
  2. 我絕不能造成DrawTest類的實例,正如我在以前的版本中那樣

所以這個源文件是更好:

#!/usr/bin/env ruby 

require "test/unit" 
require_relative "NumbersSet.rb" 

def draw_result 
    the_drawn_result = nil 
    loop do 
     the_drawn_result = rand(1000) 
     break if the_drawn_result >= 100 
    end 
    the_drawn_result 
end 

def do_operation(operation, number_1, number_2) 
    return nil if number_1 < 1 or number_2 < 1 
    number_1, number_2 = number_2, number_1 if number_1 < number_2 
    case operation 
     when :add 
      result = number_1 + number_2 
      return "#{number_1} + #{number_2} = #{result}", result 
     when :sub 
      return nil if number_1 == number_2 # A zero result will not help us 
      result = number_1 - number_2 
      return "#{number_1} - #{number_2} = #{result}", result 
     when :mul 
      return nil if number_1 == 1 or number_2 == 1 # Otherwise, this would be identity operation 
      result = number_1 * number_2 
      return "#{number_1} * #{number_2} = #{result}", result 
     when :div 
      return nil if number_1 == 1 or number_2 == 1 # Otherwise, this could be identity operation 
      return nil if number_1 % number_2 != 0 
      result = number_1/number_2 
      return "#{number_1}/#{number_2} = #{result}", result 
     else 
      raise "Unknown operation #{operation} !" 
    end 
end 

def play 
    drawn_numbers = NumbersSet.draw 
    puts drawn_numbers.inspect, draw_result 
end 

class DrawTest < Test::Unit::TestCase 
    def test_draw 

     assert_equal(nil, do_operation(:add, -3, 1)) 
     assert_equal(nil, do_operation(:add, 1, -1)) 
     assert_equal(nil, do_operation(:add, 5, 0)) 
     assert_equal(nil, do_operation(:add, 0, 5)) 

     assert_equal(nil, do_operation(:sub, -3, 1)) 
     assert_equal(nil, do_operation(:sub, 1, -1)) 
     assert_equal(nil, do_operation(:sub, 5, 0)) 
     assert_equal(nil, do_operation(:sub, 0, 5)) 

     assert_equal(nil, do_operation(:mul, -3, 1)) 
     assert_equal(nil, do_operation(:mul, 1, -1)) 
     assert_equal(nil, do_operation(:mul, 5, 0)) 
     assert_equal(nil, do_operation(:mul, 0, 5)) 

     assert_equal(nil, do_operation(:div, -3, 1)) 
     assert_equal(nil, do_operation(:div, 1, -1)) 
     assert_equal(nil, do_operation(:div, 5, 0)) 
     assert_equal(nil, do_operation(:div, 0, 5)) 

     assert_equal(nil, do_operation(:add, 3, 5)) 
     assert_equal(nil, do_operation(:add, 5, 3)) 

     assert_equal(nil, do_operation(:sub, 5, 3)) 
     assert_equal(nil, do_operation(:sub, 3, 5)) 
     assert_equal(nil, do_operation(:sub, 3, 3)) 

     assert_equal(nil, do_operation(:mul, 3, 5)) 
     assert_equal(nil, do_operation(:mul, 5, 3)) 
     assert_equal(nil, do_operation(:mul, 3, 1)) 
     assert_equal(nil, do_operation(:mul, 1, 3)) 

     assert_equal(nil, do_operation(:div, 20, 3)) 
     assert_equal(nil, do_operation(:div, 3, 20)) 
     assert_equal(nil, do_operation(:div, 20, 1)) 
     assert_equal(nil, do_operation(:div, 1, 20)) 
     assert_equal(nil, do_operation(:div, 20, 4)) 
     assert_equal(nil, do_operation(:div, 5, 20)) 

    end 
end 

OPERATIONS = [:add, :sub, :mul, :div]