我想用rspec测试我的类Episode,它在构造函数中使用一个字符串来确定剧集编号,搜索带有相应剧集编号的剧集字幕文件并设置剧集的字幕名称。
我面临的问题是我不想用真实文件进行测试,也不想创建假文件(已经做过它并且工作正常)。
我相信我需要将调用存根到Dir.glob,但到目前为止我还不走运。
有什么想法吗?
class Episode
attr_reader :avi_file, :subtitle_name, :number, :name, :directory
# an episode is instantiated with an avi filename
def initialize(avi)
@name = File.basename(avi, ".avi")
@directory = File.dirname(avi)
# Looking for an episode number in the form of
# s01e01 or 01x01
match_data = @name.match /(s\d{2,}e\d{2,}|\d{2,}x\d{2,})/i
@number = match_data.to_s
find_subtitle_in
puts self.subtitle_name
end
private
def find_subtitle_in
srt_files = Dir.glob("#{@directory}/*.srt")
@subtitle_name = srt_files.find { |e| /#{@number}/i =~ e }
end
end
功能
it "does find a subtitle" do
episode = Episode.new "Friends s01e01.avi"
Dir.stub!(:glob){["Friends.s02e01 subtitle french.srt", "Friends.s01e01 subtitle french.srt" ]}
episode.subtitle_name.should == "Friends.s01e01 subtitle french.srt"
end
rspec的输出
1)剧集确实找到了副标题 失败/错误:episode.subtitle_name.should ==“Friends.s01e01 subtitle french.srt” 预期:“Friends.s01e01 subtitle french.srt” 得到:零(使用==) #./spec/lib/episode_spec.rb:25:in'块(2级)in'
答案 0 :(得分:3)
试试这个:
it "does find a subtitle" do
Dir.stub!(:glob){["Friends.s02e01 subtitle french.srt", "Friends.s01e01 subtitle french.srt" ]}
episode = Episode.new "Friends s01e01.avi"
episode.subtitle_name.should == "Friends.s01e01 subtitle french.srt"
end