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());
}
}
}