DirectoryInfo.Exists always returns false during MSTest
Asked Answered
A

1

4

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?

Alveraalverez answered 30/11, 2011 at 19:7 Comment(0)
A
12

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;
Alexio answered 30/11, 2011 at 19:23 Comment(2)
Sure enough, this makes the tests pass. But now I need to figure out if I'm missing calls to Refresh anywhere in my app...Alveraalverez
Yes it can be a pain - I've been falling into this pitfall myself as you can see on my previous SO question: #7828632Alexio

© 2022 - 2024 — McMap. All rights reserved.