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
When faking a base class event, the fake is null.

Please try the following code:
   public class BaseClass {
      public event Action BaseClassEvent;
   }


   public class DerivedClass : BaseClass {
   }


   public class UsingClass {
      DerivedClass _derived;

      public UsingClass() {
         _derived = new DerivedClass();
         _derived.BaseClassEvent += BaseClassEvent;
      }

      void BaseClassEvent() {
      }
   }


   [TestFixture]
   public class UsingClassTest {
      [Test]
      public void TestUsingClass() {
         DerivedClass derivedClass = Isolate.Fake.Instance<DerivedClass>();
         Isolate.Swap.NextInstance<DerivedClass>().With(derivedClass);
         Action baseClassEvent = null;
         Isolate.WhenCalled(() => derivedClass.BaseClassEvent += null).DoInstead((call) => {
            baseClassEvent = (Action) call.Parameters[0];
         });
         UsingClass usingClass = new UsingClass();
         baseClassEvent();
      }
   }
asked by dwitt (680 points)

6 Answers

0 votes
Hi,

It looks like a bug in the Isolator.
The problem is that the Isolator 'see' the event adder during runtime as a BaseClass method (which is what's really happens at runtime) while the faked instance is of type DerivedClass.

The workaround is to add another fake for BaseClass
[Test]
public void TestUsingClass()
{
    var fakeDerived = Isolate.Fake.Instance<DerivedClass>();
    Isolate.Swap.NextInstance<DerivedClass>().With(fakeDerived);
    // dummy fake for BaseClass
    Isolate.Fake.Instance<BaseClass>();

    Action baseClassEvent = null;
    
    Isolate.WhenCalled(() => fakeDerived.BaseClassEvent += null).DoInstead((call) =>
    {
        baseClassEvent = (Action)call.Parameters[0];
    });

    UsingClass usingClass = new UsingClass();
    baseClassEvent();
} 


Please let me know if it helps.
answered by ohad (35.4k points)
0 votes
Your suggested workaround works for the example I gave you. However, if I make the base class abstract, the test does not pass. I am using Isolator with both abstract and non-abstract base classes.

This is a regression bug, as these scenarios worked in older versions of Isolator. The problems started to occur as a result of upgrading to Isolator 6.0 so I could work in Visual Studio 2010.

Is this bug going to be fixed soon? Can you send me a patch? I would prefer to have a fix instead of using a workaround in my code.
answered by dwitt (680 points)
0 votes
Hi,

You are correct the workaround is not instead actually fixing the bug :)
You can expect a fix in few days from now. I'll send you the patch once it's ready.
answered by ohad (35.4k points)
0 votes
Hi,

I have sent you the patch via our support system. Please let me know if it solves your problem.

Thanks!
answered by igal (5.7k points)
0 votes
The patch fixes the issue. Thank you for fixing this so quickly.

When will this be included in a released version?
answered by dwitt (680 points)
0 votes
Glad to hear it solves the problem.

Yes, this is already included in the upcoming release, which should be out soon (unfortunately, no date yet :()
answered by igal (5.7k points)
...