【发布时间】:2021-01-21 09:50:20
【问题描述】:
我必须为将在内部枚举文件和文件夹的类创建单元测试。
我当然想测试它是否正确处理未经授权访问的文件夹。
换句话说:我怀疑测试类中的方法之一会调用DirectoryInfo.EnumerateDirectories,我想确保跳过未经授权访问的文件夹。
所以我需要创建一个我的 TestObject 无权访问的文件夹。
[TestMethod]
[ExpectedException(typeof(SecurityException)]
public void HelpFileCollection_GetFiles_SkipsInaccessibleFolders()
{
// Test: HelpFileCollection.GetFiles() will skip Folders that are not accessible
// preparation: create a base Folder and a subfolder with limited security
DirectoryInfo baseFolder = this.CreateTestFolder();
DirectoryInfo subFolder = baseFolder.CreateDirectory(... ???
HelpFilderCollection testObject = new HelpFileCollection
{
RootFolder = baseFolder,
IncludeSubFolders = true,
}
// Test: call GetFiles(); expect SecurityException
testObject.GetFiles();
Assert.Fail("HelpFileCollection.GetFiles() unexpectedly didn't throw SecurityException");
}
我应该怎么做才能使子文件夹无法访问?
【问题讨论】:
标签: c# unit-testing directoryinfo unauthorizedaccessexcepti