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
0 votes
Hi,

I'm trying to Isolate with WhenCalled(...) on a non-public static method that has an overload. However, I'm not able to specify the params (telling Isolator which signature to use). It doesn't call the overloaded method (or the one with default params). Note that, for obvious reasons, I have not tried both the overloads and the default params at the same time.

1. Can I somehow specify the param (or method signature) for Isolator to use?

2. Can this work with method signatures using default params? If so, how?

Here's the code with the overload:
private static ReplyModel createReplyModel(string messageId) {...}
private static ReplyModel createReplyModel(string messageId, bool markMessageRead) {...}


Here's the code with the default param signature:
private static ReplyModel createReplyModel(string messageId, bool markMessageRead = false) {...}


TestCode with Isolator:
Isolate.NonPublic.WhenCalled(typeof(MyClass), "createReplyModel").WillReturn(null);

I am using Isolator 6.0.8.0. Thank you for your time.
asked by dblack (8.4k points)

1 Answer

0 votes
Hi,

By default, WhenCalled() affects all of the overloads of a method.
What you can do is to use the DoInstead() in the following way:

     Isolate.NonPublic.WhenCalled(typeof(ClassWithPrivateStaticMethods), "createReplyModel")
                .DoInstead(context =>
                    {
                        if (context.Parameters.Length == 2)
                        {
                            return Isolate.Invoke.Method(typeof(ClassWithPrivateStaticMethods),
                                "createReplyModel",
                                context.Parameters);                            
                        }

                        return null;
               });


Context is an object which allows us to get info about the parameters.
In this case I check the number of the parameters:
1) Two parameters were sent: Invoke createReplyModel using these parameters.
2) Outherwise : return null.

As for the second issue, you can do the same thing only change the condition:

if (c.Parameters[2].Equals(true) )
{
    return Isolate.Invoke.Method(typeof(ClassWithPrivateStaticMethods),
        "createReplyModel",
        c.Parameters);
}


Here I checked the value of the second parameter and invoked the corresponding method.

Regards,
Alex,
Typemock Support
answered by alex (17k points)
...