单元测试不工作 - Ruby 使用测试单元
Unit Tests Not Working - Ruby using test-unit
我正处于一个简单项目的最后阶段,我需要我的测试才能开始工作。基本上,我正在测试一个对数组进行排序的函数,并且 none 我的测试正在断言。我在 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
所以基本上是测试 运行,但它没有 return 任何断言。谁能指出我做错了什么或如何解决这个问题的正确方向?
你定义了一个class方法,你应该这样调用它
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"]])
我正处于一个简单项目的最后阶段,我需要我的测试才能开始工作。基本上,我正在测试一个对数组进行排序的函数,并且 none 我的测试正在断言。我在 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
所以基本上是测试 运行,但它没有 return 任何断言。谁能指出我做错了什么或如何解决这个问题的正确方向?
你定义了一个class方法,你应该这样调用它
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"]])