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

Posted by John Topley on Stack Overflow See other posts from Stack Overflow or by John Topley
Published on 2010-06-04T14:47:07Z Indexed on 2010/06/05 6:42 UTC
Read the original article Hit count: 196

Filed under:
|
|

A while ago I asked "How to test obtaining a list of files within a directory using RSpec?" and although I got a couple of useful answers, I'm still stuck, hence a new question with some more detail about what I'm trying to do.

I'm writing my first RubyGem. It has a module that contains a class method that returns an array containing a list of non-hidden files within a specified directory. Like this:

files = Foo.bar :directory => './public'

The array also contains an element that represents metadata about the files. This is actually a hash of hashes generated from the contents of the files, the idea being that changing even a single file changes the hash.

I've written my pending RSpec examples, but I really have no idea how to implement them:

it "should compute a hash of the files within the specified directory"
it "shouldn't include hidden files or directories within the specified directory"
it "should compute a different hash if the content of a file changes"

I really don't want to have the tests dependent on real files acting as fixtures. How can I mock or stub the files and their contents? The gem implementation will use Find.find, but as one of the answers to my other question said, I don't need to test the library.

I really have no idea how to write these specs, so any help much appreciated!

© Stack Overflow or respective owner

Related posts about ruby

Related posts about rspec