How do I mock the FileInfo information for a file?

Viewed 2021

I have a scenario in which I need much of the information from the FileInfo structure -- creation time, last write time, file size, etc. I need to be able to get this information cleanly, but also be able to truly unit test without hitting the file system.

I am using System.IO.Abstractions, so that gets me 99% of the way there for most of my class, except this one. I don't know how to use it to get the information I need from my MockFileSystem object.

public void Initialize(IFileSystem fs, string fullyQualifiedFileName)
{
    string pathOnly = fs.Path.GetDirectoryName(fullyQualifiedFileName);
    string fileName = fs.Path.GetFileName(fullyQualifiedFileName);

    // Here's what I don't know how to separate out and mock
    FileInfo fi = new FileInfo(fullyQualifiedFileName);

    long size = fi.LongLength;
    DateTime lastWrite = fi.LastWriteTime;
    ...
    ...
}

Any help on that line would be greatly appreciated.

UPDATE:

This is not an exact duplicate of existing questions because I'm asking how do I do it with System.IO.Abstractions, not how do I do it in general.

For those who are interested, I did find a way to accomplish it:

FileInfoBase fi = fs.FileInfo.FromFileName(fullFilePath);

If I use this line, I get the same information I need in both TEST and PROD environments.

0 Answers
Related