→ FakeFS

15 July 2009

FakeFS is a Ruby library which transparently causes File, Dir, and FileUtils to use an in-memory. faux-filesystem rather than the real filesystem on disk. Why? So you don’t have to deal with mocking libraries, so your tests are faster, and so you don’t need a hard drive to run them.

For example, this:

def test_creates_directory
  FileUtils.expects(:mkdir).with("directory").once
  Library.add "directory"
end

Becomes this:

def test_creates_directory
  Library.add "directory"
  assert File.directory?("directory")
end

I don’t know about you, but the second feels more natural to me. Also it means replacing our mkdir call with a call to mkdir_p won’t break our tests. Because, really, it shouldn’t.

Install it:

$ rip install git://github.com/defunkt/fakefs.git

And start using it in your tests:

require 'fakefs'

There really aren’t any usage examples: just write normal Ruby. If you insist, however, check out some of the Rip test or the FakeFS tests.