[rspec-users] How to mock/stub a directory of files and their contents?

John Topley john.topley at gmail.com
Tue Jun 1 07:37:25 EDT 2010


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


More information about the rspec-users mailing list