当前位置: 动力学知识库 > 问答 > 编程问答 >

ruby - How to test obtaining a list of files within a directory using RSpec?

问题描述:

I'm pretty new to the world of RSpec. I'm writing a RubyGem that deals with a list of files within a specified directory and any sub-directories. Specifically, it will use Find.find and append the files to an Array for later output.

I'd like to write a spec to test this behaviour but don't really know where to start in terms of faking a directory of files and stubbing Find.find etc. This is what little I have so far:

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

end

Any help much appreciated!

网友答案:

I don't think you need to test the library, but if you have a method like

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

you can stub the find method to return a list of files like so

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

or if you want to set the expectation then you can do

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

You can use the fakeFS gem.

分享给朋友:
您可能感兴趣的文章:
随机阅读: