2017-12-17 14 views
0

私は私のAPIをテストしようと、いくつかのユニットテストを持っている:staticメソッドが見つからない - NoMethodError - ルビー

class ClassToBeTested 

def self.something_first 
    # Do Something 
    end 

    def self.something_second 
    # Do Something 
    end 
end 

私は次のようにテストクラスにそれらを呼び出す:

class MyTest < ActionDispatch::IntegrationTest 

    test 'should get something_first' do 
    assert ClassToBeTested.something_first 
    end 

    test 'should get something_second' do 
    assert ClassToBeTested.something_second 
    end 
end 

これで、次のエラーが発生します。

Error: MyTest#test_should_get_something_first: NoMethodError: undefined method something_first' for ClassToBeTested test/services/my_test.rb:89:in block in '

bin/rails test test/services/my_test.rb:88

私は多くのことを試しましたが、問題は何か分かりません。

+0

インスタンスのメソッドは機能しますか? – user3309314

+0

まだ試してみませんでしたが、クラスには静的メソッドしかありません – user2524707

+0

おそらく何が間違っているのか調べるのに役立ちます。 – user3309314

答えて

0

私が直面した問題は、使用していた図書館に同じ名前のクラスがあることでした。

だから私のunittestのは、私が追加これを解決するために、私は定義されたクラスこの他のクラスを呼び出していませんでした。

負荷「ClassToBeTested.rb」テストクラスのヘッダに。

もっとクリーンな方法は、モジュールでClassToBeTestedを定義し、名前空間を使用して呼び出すことです。

関連する問題