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
Possible bug?

I cannot call an extension method using isolate.invoke when the target is null.

            Isolate.Fake.StaticMethods(typeof(someClass), Members.ReturnRecursiveFakes);
            Isolate.NonPublic.WhenCalled(typeof(someClass), "ValidateABN").CallOriginal();
            string target = null;
  

            //act

            //This Works
            var actual target.SomeFunction();

            //This Produces a NullReference Exception
            var actual = (bool)Isolate.Invoke.Method(typeof(someClass), "SomeFunction", target); 

            //assert
            Assert.AreEqual(false, actual);
asked by Squid (3.5k points)

4 Answers

0 votes
Hi,

I am not sure about the implementation of SomeFunction, but I'll show an example of how to invoke an extension method using Invoke API.

[TestMethod]
public void test()
{
    var someClass = new SomeClass();
    
    bool result = (bool)Isolate.Invoke.Method(typeof(SomeClassExtensions), "SomeFunction", someClass);
    
    Assert.IsTrue(result);
}


Where the extension method is:
public static class SomeClassExtensions
{
    public static bool SomeFunction(this SomeClass someClass)
    {
        return true;
    }
}


The invocation is performed on the extension method class and the argument sent is the object itself. In this example:

Isolate.Invoke.Method(
   typeof(SomeClassExtensions), // The extension methods class
   "SomeFunction", // The extension method name
   someClass) // The object on which the method needs to be invoked


Please let me know if it helps.

Regards,
Elisha,
Typemock Support
answered by Elisha (12k points)
0 votes
Elisha,

What you have is essentially what I have... My problem is that if you make the call:

Isolate.Invoke.Method(
   typeof(SomeClassExtensions), // The extension methods class
   "SomeFunction", // The extension method name
   null) // The object on which the method needs to be invoked


where you pass null in instead of someClass, then typemock throws an exception. Passing null into an extension method is supported in .Net framework.
answered by Squid (3.5k points)
0 votes
Squid,

This seems like a bug then. We query the instance that is passed to the extension method, and do not handle null properly. This will be fixed in a future version.

Thanks,
Doron
Typemock Support
answered by doron (17.2k points)
0 votes
Did you try to use Reflection instead of Typemock?
answered by pierre-luc (3.3k points)
...