单元测试不起作用 - 使用测试单元的Ruby

时间:2016-07-06 03:42:45

标签: arrays ruby unit-testing testing testunit

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

所以我有三个文件:

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 个答案:

答案 0 :(得分: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"]])
相关问题