-1

Suppose I want to test some Windows .NET code that reads, writes, and deletes files based on a directory path. Suppose that my class to test takes a DirectoryInfo parameter in its constructor. What's the best way to set up test files in memory for a .NET automated test, not on an actual disk?

My first instinct is to set up and tear down a RAM drive, but this doesn't smell right. Would such an approach be better than using the Path.GetTempPath() directory? Is there a better way to set up a directory of test files? Or does my class need to take some other abstraction of a collection of files, instead of a DirectoryInfo?

Patrick Szalapski
  • 8,738
  • 11
  • 67
  • 129
  • 2
    why not mock a directory info? – Daniel A. White Dec 09 '13 at 00:57
  • http://systemioabstractions.codeplex.com/ – Darren Kopp Dec 09 '13 at 00:58
  • @DanielA.White, how would you mock DirectoryInfo--write an IMyDirectoryInfo and an adapter to it? This isn't a pure unit test; I'd like to test the way my class integrates with DirectoryInfo. – Patrick Szalapski Dec 09 '13 at 01:04
  • Anyone have any ideas on how to make the question more specific? I believe this question can be answered in less than a few paragraphs and only would have one or a few correct answers. – Patrick Szalapski Dec 09 '13 at 13:44
  • @PatrickSzalapski I've done this in the past by creating a facade interface over the directory and file actions, that underneath are either implemented by `System.IO` or by a test implementation that uses dictionaries to store files. The only other thing I can think of is some sort of hijacking of methods, perhaps using [Microsoft Fakes](http://msdn.microsoft.com/en-us/library/hh549175.aspx) – Adam Houldsworth Dec 09 '13 at 13:52
  • Thanks Adam--a very good answer; I think it shows the question is not too broad. – Patrick Szalapski Dec 09 '13 at 18:18
  • @DanielAWhite, could you please elaborate on why you closed this question? I believe this question can be answered in less than a few paragraphs and only would have one or a few correct answers. – Patrick Szalapski Dec 16 '13 at 02:50

1 Answers1

1

Answer from Adam Houldsworth:

I've done this in the past by creating a facade interface over the directory and file actions, that underneath are either implemented by System.IO or by a test implementation that uses dictionaries to store files. The only other thing I can think of is some sort of hijacking of methods, perhaps using Microsoft Fakes.

Patrick Szalapski
  • 8,738
  • 11
  • 67
  • 129