代码之家  ›  专栏  ›  技术社区  ›  John Topley

如何使用rspec测试获取目录中的文件列表?

  •  2
  • John Topley  · 技术社区  · 15 年前

    我是RSPEC的新手。我正在编写一个rubygem,它处理指定目录和任何子目录中的文件列表。具体来说,它将使用 Find.find 并将这些文件附加到一个数组中,以便稍后输出。

    我想写一个规范来测试这种行为,但不知道从伪造文件目录和存根开始从哪里开始。 找到 等等,这是目前为止我仅有的一点:

    it "should return a list of files within the specified directory" do
    end
    

    任何帮助都非常感谢!

    2 回复  |  直到 15 年前
        1
  •  2
  •   nas    15 年前

    我认为你不需要测试这个库,但是如果你有这样的方法

    def file_names
      files = []
      Find.find(Dir.pwd){|file| files << file}
      ........
    end
    

    您可以截取find方法以返回这样的文件列表

    it "should return a list of files within the specified directory" do
      Find.stub!(:find).and_return(['file1', 'file2'])
      @object.file_names
    end
    

    或者如果你想设定期望值,你可以这样做

    it "should return a list of files within the specified directory" do
      Find.should_receive(:find).and_return(['file1', 'file2'])
      @object.file_names
    end
    
        2
  •  2
  •   shingara    15 年前

    你可以使用 fakeFS 宝石。