shouldly / shouldly

Should testing for .NET—the way assertions should be!

Home Page:https://docs.shouldly.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Support ShouldExist on file or directory based objects

jeffkl opened this issue · comments

Some of my tests expect files to exist after calling something. I have extension methods for this but I would love it to be built in.

https://github.com/jeffkl/MSBuildProjectCreator/blob/8e0a219aa7025f8c0ade8d5e10c8e917b8b8d6b4/src/Microsoft.Build.Utilities.ProjectCreation.UnitTests/ExtensionMethods.cs#L10-L31

Calling fileInfo.Exists.ShouldBeTrue() isn't helpful in my opinion. Having the file or directory path listed is much better.

public static FileInfo ShouldExist(this FileInfo fileInfo)
{
    if (!fileInfo.Exists)
    {
        throw new ShouldAssertException($"The file \"{fileInfo.FullName}\" should exist but does not");
    }

    return fileInfo;
}
FileInfo fileInfo = new FileInfo("some path");
classUnderTest.MethodUnderTest(fileInfo);
fileInfo.ShouldExist();