FileOrDirectoryExists Constraint
FileOrDirectoryExistsConstraint tests that a file or directory exists at the specified path.
Usage
Does.Exist
Does.Not.Exist
Modifiers
.IgnoreDirectories // Only check for file existence
.IgnoreFiles // Only check for directory existence
Examples
[Test]
public void FileOrDirectoryExistsConstraint_Examples()
{
var tempFile = Path.GetTempFileName();
var tempDir = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
Directory.CreateDirectory(tempDir);
try
{
// Test file or directory existence
Assert.That(tempFile, Does.Exist);
Assert.That(tempDir, Does.Exist);
Assert.That("nonexistent.txt", Does.Not.Exist);
// Test with FileInfo/DirectoryInfo
Assert.That(new FileInfo(tempFile), Does.Exist);
Assert.That(new DirectoryInfo(tempDir), Does.Exist);
}
finally
{
File.Delete(tempFile);
Directory.Delete(tempDir);
}
}
Notes
- The constraint accepts string paths,
FileInfo, orDirectoryInfoobjects. - By default, the constraint passes if either a file or directory exists at the path.
- Use
.IgnoreDirectoriesto require a file specifically. - Use
.IgnoreFilesto require a directory specifically.
See Also
- EmptyDirectory Constraint - Test if directory is empty
- SamePath Constraint - Compare paths