1

我想用 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" got: nil (using ==) # ./spec /lib/episode_spec.rb:25:in `block (2 levels) in '

4

1 回答 1

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
于 2012-07-05T21:57:59.633 回答