chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
Welcome to Typemock Community! Here you can ask and receive answers from other community members. If you liked or disliked an answer or thread: react with an up- or downvote.
0 votes
I have a class with the same method:
class MyClass
{
        ...............
        private static MyClass MeMethod(string myparam)
        {
            MyClass result = null;
            ............
            string[] dir = Directory.GetFiles(myparam);
            .........
            return result;
        }
        ................
}

And I need to test it with isolating Directory.GetFiles call. I've tried something like this:
var myClassFake = Isolate.Fake.Instance<MyClass>(Members.CallOriginal);
Isolate.Fake.StaticMethods(typeof(Directory));

var str1 = "some path";
var str2 = "some pattern";
Isolate.WhenCalled(() => Directory.GetFiles(str1, str2)).WillReturn(null);
Utilities.Throws<DirectoryNotFoundException>(() =>
                    Isolate.Invoke.Method(typeof(MyClass), "MyMethod", myparam),
                    "", "Invalid msg on validation error");


but it wants work. It throws a DirectoryNotFoundException in line
Isolate.WhenCalled(() => Directory.GetFiles(str1, str2)).WillReturn(null);


You solutions can be?
asked by atikhomirov (1.6k points)

1 Answer

0 votes
Hi,

Directory.GetFiles() is in mscorlib.dll, which currently cannot be faked with Isolator (except for specific APIs that are enabled, like DateTime.Now). This means that the WhenCalled() statement does not record the call, and the framework actually tries to run GetFiles(), which causes the exception you got.

As a workaround until we provide a complete mscorlib support you can wrap your IO logic in a method and fake that method.

Doron
Typemock Support
answered by doron (17.2k points)
...