DirectoryInfo.Exists在MSTest中始终返回false [英] DirectoryInfo.Exists always returns false during MSTest

查看:138
本文介绍了DirectoryInfo.Exists在MSTest中始终返回false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序处理创建目录的边界有一点逻辑。我想测试它实际上按预期创建目录,但是 DirectoryInfo.Exists 属性始终返回false,即使该目录实际存在。



另请参见此问题 - 您需要设置一个断点来查看该目录是否实际创建,因为MSTest将在测试结束时将其删除。



有没有一些设置告诉MSTest在测试期间允许正常文件系统IO?

解决方案假设您稍早创建DirectoryInfo实例,则有一些内部缓存涉及目录状态 - 如果调用 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆