简体   繁体   English

System.IO.Abstractions 模拟文件删除

[英]System.IO.Abstractions Mock File Delete

I'm trying to test that some files were deleted.我正在尝试测试是否删除了某些文件。 This is the code I'm testing.这是我正在测试的代码。

public void RemoveLogFiles()
{
    MainLogic.LogMessage("** Performing Housekeeping **");

    // Delete Old Logfiles
    string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
    path += logsDirectory;

    string[] files = _fileSystem.Directory.GetFiles(path);
    int span = Project.Properties.Settings.Default.LogRetention * -1;

    foreach (string file in files)
    {
        FileInfoBase fi = (FileInfoBase)_fileSystem.FileInfo.FromFileName(file);
        if (fi.LastAccessTime < DateTime.Now.AddDays(span))
        {
            MainLogic.LogMessage("Delete " + fi.FullName);
            fi.Delete();
        }
    }
}

And Here is my test.这是我的测试。

var mockDirectory = new Mock<IDirectory>();
string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
path += MainWindow.logsDirectory;

string file1 = path + "\\test.log";
string file2 = path + "\\test2.log";

mockDirectory.Setup(g => g.GetFiles(path)).Returns(new[] { file1, file2 });

var _fileSystem = new Mock<IFileSystem>();
_fileSystem.SetupGet(g => g.Directory).Returns(mockDirectory.Object);

MainWindow window = new MainWindow(_fileSystem.Object);
window.RemoveLogFiles();

Notes笔记

  • I've verified that the expected path in my mock is the same as the path that is and will be passed in prod.我已经验证了我的模拟中的预期路径与在 prod 中传递的路径相同。

Questions / Problems问题/问题

  1. GetFiles() does not return anything even though I've mocked it to return test.log and test2.log . GetFiles()不返回任何东西,即使我已经模拟它返回test.logtest2.log

  2. Do I need to mock the file as well or is there a better way to test file deletes?我还需要模拟文件还是有更好的方法来测试文件删除?

You can setup a looser expectation using It.IsAny<string>()您可以使用It.IsAny<string>()设置更宽松的期望

//Arrange
string[] files = new[] { file1, file2 };
var _fileSystem = new Mock<IFileSystem>();
_fileSystem
    .Setup(_ => _.Directory.GetFiles(It.IsAny<string>())
    .Returns(files);

//... still need to setup file info for
//... _fileSystem.FileInfo.FromFileName(file)

MainWindow window = new MainWindow(_fileSystem.Object);

//Act
window.RemoveLogFiles();

//Assert

and see if that behaves as expected.看看它的行为是否符合预期。

If it dos then there was an issue with the path .如果确实如此,则path存在问题。

Do I need to mock the file as well我是否也需要模拟文件

Yes if you want the subject under test to flow to completion as expected.是的,如果您希望被测主题按预期完成。

Finally I would also suggest using Path.Combine instead when constructing file paths最后,我还建议在构建文件路径时使用Path.Combine

string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
path = Path.Combine(path, logsDirectory);

// or single line

string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop, logsDirectory);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM