tags:

views:

85

answers:

2

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!

+2  A: 

You can use the fakeFS gem.

shingara
+2  A: 

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
nas
Great, thanks nas. And a good point about not needing to test the library.
John Topley