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
Hi all,

i'm beginning with typemock and I would like to mock a static method, but only the cases I've not implemented.

Let looks at this example :

    class Program
    {
        static void Main()
        {
            Isolate.WhenCalled(() => MyStaticClass.GetValue("test")).WillReturn(0);
            Isolate.WhenCalled(() => MyStaticClass.GetValue("foo")).WillReturn(1);

            Console.WriteLine(MyStaticClass.GetValue("test"));
            Console.WriteLine(MyStaticClass.GetValue("foo"));
            Console.WriteLine(MyStaticClass.GetValue("else"));
        }
    }

    public static class MyStaticClass
    {
        public static int GetValue(string value)
        {
            if (value == "else")
                return 3;
            throw new NotImplementedException();
        }
    }


it returns me :
0
1
1


and I would like to have :
0
1
3


of course, I can add a new requirement at the beginning, but If my code becomes bigger, I don't want to Isolate each case at the beginning.

How can I do ?

Thanks for your help
asked by timor.super (1.9k points)

2 Answers

0 votes
Hi Timor, and welcome to the wonderful world of Typemock!

What you are trying to do is called behavior sequencing. That means you want to set a different behavior on each call to a method. The golden rule of behavior sequencing is that the final behavior setting remains the default for all subsequent calls. That's why the third call returns 1 - the last setting is to return 1.

(by the way, although you put the arguments inside the WhenCalled clause, Isolator disregards them- it just counts calls).

So how do we do this?

If you want the third call to exercise the real code, add a CallOriginal. In this example, I've removed the arguments, replaced them with empty string, because, like I said only the order of call counts:

Isolate.WhenCalled(() => MyStaticClass.GetValue("")).WillReturn(0); 
Isolate.WhenCalled(() => MyStaticClass.GetValue("")).WillReturn(1); 
Isolate.WhenCalled(() => MyStaticClass.GetValue("")).CallOriginal();


That will give you the result you want. But I think you're interested in another feature - you probably want to make sure when the argument is "else", then the real code will run. This is called conditional behavior. Here you specify the argument, with an addition to tell Isolator to run it only in the case of the correct arguments:

Isolate.WhenCalled(() => MyStaticClass.GetValue("")).WillReturn(0); 
Isolate.WhenCalled(() => MyStaticClass.GetValue("")).WillReturn(1);
Isolate.WhenCalled(() => MyStaticClass.GetValue("else")).WithExactArguments().CallOriginal();


This time we're telling Isolator to do CallOriginal only if the argument is "else".

I hope this was helpful. Let em know if you need further help.

Thanks,
answered by gilz (14.5k points)
0 votes
Thansk Gilz for your help,

CallOriginal is exactly what I need.

I didn't know that the arguments are ignored, except if you call WithExactArguments.

Best regards,

Tim
answered by timor.super (1.9k points)
...