2017-12-17 5 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

제가 직면 한 문제는 사용하고 있던 라이브러리에 같은 이름의 클래스가 있다는 것입니다.

로드 'ClassToBeTested.rb'테스트 클래스의 헤더 :

그래서 내 유닛 테스트는 내가 추가 한이 문제를 해결하기 위해이 다른 클래스가 아니라 내가 정의 된 클래스를 호출했다.

더 깨끗한 방법은 모듈에서 ClassToBeTested를 정의하고 네임 스페이스를 사용하여 호출하는 것입니다.

관련 문제