You should create a wrapper service called IFileService, then you can create a specific one that uses statics for use in your application, and a dummy IFileService that will have fake functions for testing. Make it so that you need to pass the IFileService to the constructor or property for what the class ever uses, so for normal operation you need to go to the IFileService. Remember that in unit testing, you check only that part of the code, and not that it is called as IFileService.
interface IFileService { bool Exists(string fileName); void Delete(string fileName); } class FileService : IFileService { public bool Exists(string fileName) { return File.Exists(fileName); } public void Delete(string fileName) { File.Delete(fileName); } } class MyRealCode { private IFileService _fileService; public MyRealCode(IFileService fileService) { _fileService = fileService; } void DoStuff() { _fileService.Exists("myfile.txt"); } }
source share