测试验证 ruby 子类实现策略方法
我正在实现一个简单的策略模式(第一次在 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
我目前正在使用 rspec、factory_girl 和 Shoulda-matchers 进行测试。有人知道我如何实现这一目标吗?
干杯!
PS 我习惯于用 C# 指定一个字面“接口”,所以这就是为什么我想看看我可以在 ruby 中使用什么来为每个策略强制执行一个通用接口...
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...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我希望它会是这样的,
I would expect it to be something like,