How do I best set up a directory of test files in memory instead of disk, in .NET, for testing a file IO class?

StackOverflow https://stackoverflow.com/questions/20461221

Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top