2011-09-12 2 views
0

루비에서 처음으로 간단한 전략 패턴을 구현 중이며 모든 하위 클래스가 중요한 전략 방법을 구현하는지 테스트를 작성하고 싶습니다. 나는 현재 내 테스트를 위해 RSpec에, factory_girl 및 했어야-매처 (matcher)를 사용하고ruby ​​서브 클래스의 유효성을 검사하는 테스트를 통해 전략 메소드

class SearchTools::MusicSearcher 
    def find_artists 
    raise 'Abstract method called' 
    end 
end 

class SearchTools::LastFMSearcher < MusicSearcher 
    def find_artists(search_phrase) 
    # get artists from lastfm's restful api 
    end 
end 

class SearchTools::DatabaseSearcher < MusicSearcher 
    def find_artists(search_phrase) 
    # look in database for artists 
    end 
end 

class SearchTools::Search 

    def initialize(searcher) 
    @searcher = searcher 
    end 

    def find_artists(search_phrase) 
    @searcher.find_artists(search_phrase) 
    end 

end 

: 그래서, 나는 이런 식으로 뭔가가있다. 아무도 내가 이걸 어떻게 달성했는지 알아?

건배!

P. 저는 C#으로 리터럴 '인터페이스'를 지정하는 데 익숙합니다. 그래서 각 전략에 공통 인터페이스를 적용하기 위해 루비에서 사용할 수있는 것을 찾고 있습니다 ...

+0

'respond_to? '를 사용 하시겠습니까? –

+0

Dave 응답에 감사드립니다. respond_to는 좋아 보이지만 내 초록 MusicSearcher의 하위 클래스에이 테스트를 적용하는 방법은 무엇입니까? – Stu

+0

http://snippets.dzone.com/posts/show/2992 –

답변

0

나는 그것이 예상됩니다.

it "should respond to find_artists method" do 
    o = SearchTools::LastFMSearcher.new 
    o.respond_to?(:find_artists).should be_true 
end 
관련 문제