单元测试不起作用——Ruby使用test-unit



我正处于一个简单项目的最后阶段,我需要我的测试开始工作。基本上,我正在测试一个对数组进行排序的函数,并且我的测试都没有断言。我正在使用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。

$ 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

基本上测试运行了,但是它没有返回任何断言。谁能指出我在正确的方向,我做错了什么,或者如何解决这个问题?

您已经定义了一个类方法,您应该像

那样调用它
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"]])

最新更新