问题描述
我的应用程序处理创建目录的边界有一点逻辑。我想测试它实际上按预期创建目录,但是属性始终返回false,即使该目录实际存在。另请参见 - 您需要设置一个断点来查看该目录是否实际创建,因为MSTest将在测试结束时将其删除。
有没有一些设置告诉MSTest在测试期间允许正常文件系统IO?
DirectoryInfo.Refresh()
来强制更新这应该工作: var dir = new DirectoryInfo(@。\someDir );
//...other something here
dir.Refresh();
bool doesExist = dir.Exists;
I have a little bit of logic at the boundary of my app dealing with creating directories. I would like to test that it actually creates directories as expected, but the DirectoryInfo.Exists property always returns false even when the directory actually exists.
See also this question - you need to set a breakpoint to see that the directory has actually been created because MSTest will delete it when the test ends.
Is there some setting that tells MSTest to allow "normal" filesystem IO during tests?
Assuming you create the DirectoryInfo instance somewhat earlier there is some internal caching of directory state involved - if you call DirectoryInfo.Refresh()
to force an update this should work:
var dir = new DirectoryInfo(@".\someDir");
//...other things here
dir.Refresh();
bool doesExist = dir.Exists;
这篇关于DirectoryInfo.Exists在MSTest中始终返回false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!