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 - I have a class that looks like this:

    public class a
    {
        //some function
    }
    public static class classA
    {
        public static void dostuff1(this a aobj)
        {
            aobj.doPrivateStuff1();
            aobj.doPrivateStuff2();
        }

        public static void dostuff2(this a aobj)
        {
            aobj.doPrivateStuff2();
            aobj.doPrivateStuff3();
        }

        private static void doPrivateStuff1(this a obj)
        {

        }
        private static void doPrivateStuff2(this a obj)
        {

        }
        private static void doPrivateStuff3(this a obj)
        {

        }
    }



How do I successfully test a.doStuff1 to confirm that doPrivateStuff1 & 2 were called? I tried this, but no joy:

        [TestMethod]
        public void dostuff1_calls_doprivatestuff1and2()
        {
            var target = new a();
            Isolate.Fake.StaticMethods<a>();
            target.dostuff1();
            Isolate.Verify.NonPublic.WasCalled(target, "doPrivateStuff1");
            Isolate.Verify.NonPublic.WasCalled(target, "doPrivateStuff2");
        }
asked by Squid (3.5k points)

6 Answers

0 votes
Hi,
It looks like a bug - the Isolator fails recognize the extension methods of "classA" as class "a" methods.

As workaround you should fake the static methods of classA and do the verification on classA:
[Test]
public void dostuff1_calls_doprivatestuff1and2()
{
    Isolate.Fake.StaticMethods(typeof(classA), Members.CallOriginal);

    var target = new a();
    target.dostuff1();

    Isolate.Verify.NonPublic.WasCalled(typeof(classA), "doPrivateStuff1");
    Isolate.Verify.NonPublic.WasCalled(typeof(classA), "doPrivateStuff2");
}


Please let me know if it helps.
answered by ohad (35.4k points)
0 votes
Ohad - Many many thanks!!! I had almost given up on this! Even my tech lead said to just make the methods public - but I wasn't satisfied with that solution!!

In my production code, doStuff1 is actually a private member, but I got it working using:

edit: removed private accessor.

[Test]
public void dostuff1_calls_doprivatestuff1and2()
{
  var target = new a();
  Isolate.Fake.StaticMethods(typeof(classA),Members.CallOriginal);

  Isolate.Invoke.Method(typeof(classA), "dostuff1",new object [] {target});

  Isolate.Verify.NonPublic.WasCalled(typeof(classA), "doPrivateStuff1");
  Isolate.Verify.NonPublic.WasCalled(typeof(classA), "doPrivateStuff2");
} 
answered by Squid (3.5k points)
0 votes
Hi Ohad,

Even though it has a non-obvious workaround, you mentioned that this issue is a bug. I am using v7.0.8 and it still isn't fixed. Any idea when this will be fixed?

Thanks for your time.
answered by dblack (8.4k points)
0 votes
Hi Dave,

I will check our backlog and will update you ASAP.
answered by alex (17k points)
0 votes
Hi Dave,

This behavior is not changed in the new version. We're not going to change it yet since it can lead to ambiguous behavior - Extension methods are basically just a static methods belong to a different type. Some could expect the verification be done on the static class itself while others expect to verify it on the instance.

Currently, the recommended practice for verifying a private extension method is to verify it against the declaring type.
answered by alex (17k points)
0 votes
Hi Alex,

Thank you for the update. I can see how there could be some ambiguity. I'll continue testing as you've suggested.

Thank you for your time.
answered by dblack (8.4k points)
...