2016-07-06 11 views
1

私は単純なプロジェクトの最終段階にあり、テストを開始する必要があります。基本的には、配列をソートする関数をテストしていますが、私のテストでは何も宣言していません。私はRubyでテストユニットの宝石を使用しています。ユニットテストが動作しない - テストユニットを使用している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を覚えておくことが好きではない

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

のようにそれを呼び出す必要があり、テストは最終段階のためではありません!彼らは非常に始まっています。最初にテストを書く、後でコードを書く! –

答えて

4

あなたはクラスメソッドを定義している、あなたは

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

ありがとうございます!これは私のすべてのテストを動作させるために必要な正確な答えです! –

関連する問題