2016-07-06 65 views
1

我正处于一个简单项目的最后阶段,我需要测试才能开始工作。基本上,我正在测试一个排序数组的函数,而且我的测试都没有断言。我在Ruby中使用测试单元gem。单元测试无法正常工作 - Ruby使用测试单元

所以我有三个文件:

program.rb (where the method is invoked and passed an array) 
plant_methods.rb (where the class is defined with its class method) 
tc_test_plant_methods.rb (where the test should be run) 

继承人了解每个文件:

plant_methods.rb 

plant_sort的目的是每个子阵列按字母顺序排序,利用第一工厂在子 - 阵列。

class Plant_Methods 
    def initialize 
    end 

    def self.plant_sort(array) 
    array.sort! { |sub_array1, sub_array2| 
     sub_array1[0] <=> sub_array2[0] } 
    end 
end 

这里是程序文件。

program.rb 

require_relative 'plant_methods' 

plant_array = [['Rose', 'Lily', 'Daisy'], ['Willow', 'Oak', 'Palm'], ['Corn', 'Cabbage', 'Potato']] 

Plant_Methods.plant_sort(plant_array) 

这里是测试单元。

tc_test_plant_methods.rb 

require_relative "plant_methods" 
require "test/unit" 

class Test_Plant_Methods < Test::Unit::TestCase 

    def test_plant_sort 
     puts " it sorts the plant arrays alphabetically based on the first plant" 
    assert_equal([["Gingko", "Beech"], ["Rice", "Wheat"], ["Violet", "Sunflower"]], Plant_Methods.new([["Violet", "Sunflower"], ["Gingko", "Beech"], ["Rice", "Wheat"]]).plant_sort([["Violet", "Sunflower"], ["Gingko", "Beech"], ["Rice", "Wheat"]])) 
    end 

end 

然而,当我运行tc_test_plant_methods.rb,我得到以下错误:

$ ruby tc_plant_methods.rb 
Run options: 
# Running tests: 

[1/1] Test_Plant_Methods#test_plant_sort it sorts the plant arrays alphabetically based on the first plant 
= 0.00 s 
    1) Error: 
test_plant_sort(Test_Plant_Methods): 
ArgumentError: wrong number of arguments (1 for 0) 

而且

Finished tests in 0.003687s, 271.2232 tests/s, 0.0000 assertions/s. 
1 tests, 0 assertions, 0 failures, 1 errors, 0 skips 

所以基本上在测试运行,但它不返回任何断言。任何人都可以指出我正确的方向,我做错了什么,或者如何解决这个问题?

+1

只是要记住,公主@Leia_Organa,测试是不是最后阶段!他们刚刚开始。先写测试,稍后再写代码! –

回答

4

您已经定义一个类的方法,你应该称呼它

Plant_Methods.plant_sort([["Violet", "Sunflower"], 
         ["Gingko", "Beech"], ["Rice", "Wheat"]]) 

不喜欢

Plant_Methods.new([["Violet", "Sunflower"], ["Gingko", "Beech"], ["Rice", "Wheat"]])\ 
      .plant_sort([["Violet", "Sunflower"], ["Gingko", "Beech"], ["Rice", "Wheat"]]) 
+0

谢谢!这是我所有测试工作所需的确切答案! –