How to mock/stub a directory of files and their contents?

Hi,

I’m writing a gem that enumerates the files within a specified
directory and sub-directories. It’s a module that has a single class
method that returns an array containing the file names, but that array
will also contain elements that represent metadata about the files.
For example, one element will be a hash that’s computed from the
contents of each file, so that if a file is changed a different hash
will be returned.

I’m pretty new to RSpec and the thing I am struggling with is how to
mock or stub not only the files, but also their contents. I know that
the implementation will use Find.find, but I don’t want to give it a
real directory of fixture files for the specs to use because then how
will I change the contents of a file to test that a different hash is
returned?

These are the pending examples I have so far:

it “should return a list of files within the specified directory”
it “should compute a hash of the files within the specified directory”
it “should compute a different hash if the content of a file changes”
it “shouldn’t include hidden files or directories within the specified
directory”

Any help would be greatly appreciated!

John

On Jun 1, 2010, at 7:37 AM, John T. wrote:

I’m pretty new to RSpec and the thing I am struggling with is how to
it “should compute a different hash if the content of a file changes”
it “shouldn’t include hidden files or directories within the specified
directory”

Any help would be greatly appreciated!

Take a look at GitHub - fakefs/fakefs: A fake filesystem. Use it in your tests.. It stubs the file
system for you, allowing you to create files in an in-memory file system
that then gets blown away after the example.

HTH,
David