开发者

Test to validate ruby subclasses implement strategy method

I'm implementing a simple strategy pattern (for the first time in ruby) and I want to write a test to make sure that every subclass implements the crucial strategy method. So, I have something like this:

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

I'm currently using rspec, factory_girl and shoulda-matchers for my testing. Anyone know how I achieve this?

Cheers!

P.S. I'm used to specifying a literal 'interface' with C#, so that's why I'm looking to see what I can use in ruby to enforce a common interface for each strategy...


I would expect it to be something like,

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜