The Temp Files Pattern

This is a pattern I first started using while working on rublog w/ Brian Marick et al. Dave Thomas gave it a better name when he checked it in, and I’ve renamed it since then. I think I like temp files. A second ago, I was working in nfit, and found myself writing it in c#. So I guess I like it.


You want to test something that accesses files. Something that you want to do all the time for some apps.


Create a class called !TempFiles, use it like this (for ruby):

def test_load
	files =
	files.add('wiki/captions/2003_11-November_captions.txt', CONTENTS)
	files.create do |dir|
		captions =, '/2003/11-November'), '/2003/11-November')
		assert_equal('Jilian', captions['apPle.JPG'])
		assert_equal('from the top', captions['bitter.jpg'])

It can be used from any test, and makes creating a file easy. From ruby the blocks make it really nice. I often add files in the setup code, but always have the block in the actual test. The nice thing about this is it’s easy to add files in your test right before creating them.

I’ll add the c# usage tomorrow

  • UPDATE *

this is now a gem called file_sandbox – sudo gem install file_sandbox – enjoy…

Tags: , , ,

Comments are closed.