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

문제

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?

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top