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开发者_JAVA百科 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.
精彩评论