1
我想用rspec测试我的类Episode,它会在构造函数中采用一个字符串来确定插曲编号,搜索带有相应插曲编号的插曲字幕文件并设置字幕名称的情节。红宝石存根一个Dir.glob与Rspec
我面临的问题是,我不想用真实文件测试既不创建假文件(已经做到了,它工作正常)。
我相信我需要将呼叫存根到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” expected:“Friends.s01e01 subtitle french.srt” got:nil(using ==) #./spec/lib/episode_spec.rb:25:in`block(2 levels )在“