Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DirectoryInfo.Exists always returns false during MSTest

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?

like image 655
default.kramer Avatar asked Dec 27 '22 10:12

default.kramer


1 Answers

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;
like image 190
BrokenGlass Avatar answered Jan 04 '23 10:01

BrokenGlass