public void CleanupHandlesFileExceptions() { IFileService fileServiceProvider = Substitute.For <IFileService>(); MemoryStream memoryStream = new MemoryStream(); fileServiceProvider.CreateFileStream("").ReturnsForAnyArgs(memoryStream); IDirectoryInformation directoryInfo = Substitute.For <IDirectoryInformation>(); IFileInformation fileInfo = Substitute.For <IFileInformation>(); directoryInfo.EnumerateChildren().ReturnsForAnyArgs(new IFileInformation[] { fileInfo }); fileServiceProvider.GetPathInfo("").ReturnsForAnyArgs(directoryInfo); using (FileCleaner cleaner = new FileCleaner("Test", fileServiceProvider)) { cleaner.TrackFile("Bar"); fileServiceProvider.WhenForAnyArgs(f => f.DeleteFile("")).Do(a => { throw new Exception("TestException"); }); } }