I've been writing a series of small Ruby programs that compare strings, arrays, etc. I'm currently learning how to write small unit tests for my methods. I've been able to write simple tests that perform functions on strings, but when it comes to more difficult tests, like tests to perform functions on arrays, I've got no idea. If anyone could help me with this current question, I'd really appreciate it.
So let's say I have three files in my folder: program.rb, plant_methods.rb, and tc_plant_methods.rb
Here's what's in plant_methods.rb. It contains a class with two methods available in it. The plant_sort method loops through an array of arrays and alphabetizes them according to the first item in the array.
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
Here is the code in program.rb
require_relative 'plant_methods'
plant_array = [['Rose', 'Lily', 'Daisy'], ['Willow', 'Oak', 'Palm'], ['Corn', 'Cabbage', 'Potato']]
Plant_Methods.plant_sort(plant_array)
print plant_array
And here is the output when program.rb is run. As you can, each sub-array is now alphabetized according to the first element:
[["Corn", "Cabbage", "Potato"], ["Rose", "Lily", "Daisy"], ["Willow", "Oak", "Palm"]]
Now, my question is: how do I write a unit test for this?? Here's what's currently in my tc_plant_methods.rb file:
require_relative "plant_methods"
require_relative "program"
require "test/unit"
class Test_Plant_Methods < Test::Unit::TestCase
def test_plant_sort
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
I keep getting errors when I try to run the unit test. Am I doing something wrong here that anyone can identify? Here's the first error that rises when I try to run the test:
tc_plant_methods.rb:8: syntax error, unexpected tCONSTANT, expecting ')'
Aucun commentaire:
Enregistrer un commentaire