Welcome to Typemock Answers. Here you can ask and receive answers from other community members. And if you liked or disliked an answer or thread: react with an up- or downvote Enjoy!

How to verify that generic private method was called?

0 votes
Hello.

I need to verify that private static generic method was called with the given type parameter.

Yet
Isolate.Verify.NonPublic.WasCalled()
has no overloads where I could specify type parameters.

Is such verification possible?
asked May 30, 2014 by voroninp (300 points)

6 Answers

0 votes
Hi,

I reproduced this issue and it seems to be a bug.
We're now looking into it. I'll keep you posted.

 

Alex Galin | Support Team

answered Jun 15, 2014 by alex (17,910 points)
0 votes
Hi,

This isn't actually a bug but a missing feature.
However there is a workaround for this using the DoInstead() API, See the following example:
Code under test:
public class Foo
{
    public static void CallWithInt()
    {
        Bar<Int32>();
    }

    private static void Bar<T>()
    {
    }
}

Test:
[TestMethod]
public void TestMethod1()
{
    bool wasCalledWithInt = false;
    bool wasCalledWithChar = false;

    Isolate.NonPublic.WhenCalled(typeof(Foo) , "Bar").DoInstead(context =>
        {
            Type[] genericArguments = context.Method.GetGenericArguments();
            if (genericArguments[0] == typeof(Int32))
            {
                wasCalledWithInt = true;
            }
            if (genericArguments[0] == typeof(char))
            {
                wasCalledWithChar = true;
            }

            context.WillCallOriginal();
        });

    Foo.CallWithInt();

    Assert.IsTrue(wasCalledWithInt);
    Assert.IsFalse(wasCalledWithChar);
}


Let me know if it helps

 

Alex Galin | Support Team

answered Jun 23, 2014 by alex (17,910 points)
0 votes
Hi,
We added API to verify static generic methods:
Isolate.Verify.NonPublic.WasCalled(Type type, string methodName, params Type [] methodGenericArguments)
The last parameters is an array of types for the generic method.
Example:

// in code under test
TestPrivates.PrivateStaticGenericMethod<int>()
// in test code.
Isolate.Verify.NonPublic.WasCalled(typeof (TestPrivates), "PrivateStaticGenericMethod", typeof(int));


You can download the patch from here:
answered Jul 1, 2014 by ohad (37,080 points)
0 votes
thanks
answered Aug 4, 2014 by voroninp (300 points)
0 votes
see no such api in v7.5.5.0
answered Sep 19, 2014 by voroninp (300 points)
0 votes
Hi,

You can find it in Isolator V8

 

Alex Galin | Support Team

answered Sep 29, 2014 by alex (17,910 points)
...