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
0 votes
I have a fake that fires a fake event with DoInstead. When doing it this way the the closure does not have access to the appropriate context. If I fire the event manually outside of DoInstead, the closure operates in the appropriate context.


Code to repro the bug is below. One test fails and one passes. Both should pass. Please let me know if you need more information but this should be enough.

#region Using Statements
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TypeMock.ArrangeActAssert;

#endregion

namespace TypeMock.BugRepro{

   public class TestClass {
      public IPublisher Publisher {get;set;}
      public IService Service {get;set;}

      public void DoSomething() {

         bool succcess = true;
         this.Publisher.Published += delegate {
            if (succcess) {
               this.Service.Execute();
            }
         };

         this.Publisher.Publish();
      }
   }

   public interface IService {
      void Execute();
   }

   public interface IPublisher {
      void Publish();
      event System.EventHandler<EventArgs> Published;
   }

   [TestClass]
   public class TestClassFixture{

      /// <summary>
      /// Verifies that the service was executed when then published event was fired
      /// with a separate call to Isolate.Invoke
      /// </summary>
      [TestMethod, Isolated]
      public void DoSomething_ServiceExecuted_OnPublished(){
         //arrange
         var service = Isolate.Fake.Instance<IService>();
         var publisher = Isolate.Fake.Instance<IPublisher>();
         var testClass = new TestClass{Service = service,Publisher = publisher};

         //act
         testClass.DoSomething();

         //Note:firing the event with isolater here works
         Isolate.Invoke.Event(() => publisher.Published += null, null, null);

         //assert
         Isolate.Verify.WasCalledWithAnyArguments(()=>service.Execute());

      }

      /// <summary>
      /// Verifies that the service was executed when then published event was fired
      /// with a DoInstead that calls Isolate.Invoke
      /// </summary>
      [TestMethod, Isolated]
      public void DoSomething_ServiceExecuted_OnPublishWithDoInstead() {
         //arrange
         var service = Isolate.Fake.Instance<IService>();
         var publisher = Isolate.Fake.Instance<IPublisher>();
         var testClass = new TestClass{Service = service,Publisher = publisher};

         //Note:arranging the event exection here does not work
         Isolate.WhenCalled(() => publisher.Publish()).DoInstead(callContext => Isolate.Invoke.Event(() => publisher.Published += null, null, null));

         //act
         testClass.DoSomething();

         //assert
         Isolate.Verify.WasCalledWithAnyArguments(() => service.Execute());
      }
   }
}

asked by juice_johnson (9.8k points)

2 Answers

0 votes
Hi,

The 2nd test seems fine and should work. We'll get a local repro going and try to recreate the issue.

Doron
Typemock Support
answered by doron (17.2k points)
0 votes
Hello,

We've investigated the issue you're reported. This was a bug and it was already solved for our upcoming version release of Isolator. I've sent you a link to the patched version via our support system. Please let me know if it solves your problem.

Thanks!
answered by igal (5.7k points)
...