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 need to do the equivalent of the below on a non public static method. How would I do this? By the way, I can't use InternalsVisibleTo (long drawn out story), so that wouldn't be an option.

Isolate.NonPublic.WhenCalled(target, "StaticFactoryMethod").WillReturn(target);
asked by boo (21.8k points)

3 Answers

0 votes
Hi Boo,

The Syntax for static methods is pretty much the same, but because static calls are made on types rather than instances, we pass in the type of the class we want to fake behavior for:

Isolate.NonPublic.WhenCalled(typeof(FactoryClass), "StaticFactoryMethod").WillReturn(something);


Please let me know if this helps.

Thanks,
Doron
Typemock Support
answered by doron (17.2k points)
0 votes
I thought that would work, but then I went back and realized the static type itself is private (and again can't use InternalsVisibleTo for obvious reason and private accessor don't give you access to private child classes) - that's the problem...

public class Foo
{
    public void TargetTestMethod()
    {
        bool b = Bar.MethodIWantToCheckIsCalled()
        //Some other work
    }

    private static class Bar
    {
        public bool MethodIWantToCheckIsCalled()
        {
        }
    }
}
answered by boo (21.8k points)
0 votes
Hi Boo,

Unfortunately, we can't fake a type we can't access like that. Perheps you could retrieve the type by reflection and pass it to WhenCalled()?

Try this out:
var type = typeof(Foo).GetNestedType("Bar", BindingFlags.NonPublic);


And let me know if it worked.

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