I have come across an issue which mixes up the firing of two eventhandlers of the same name under the same interface. It starts with the following implementation
public class BaseArgs : EventArgs
{
}
public interface IBaseInterface
{
event EventHandler<BaseArgs> Handler;
}
public class ExtendedArgs : BaseArgs
{
}
public interface IExtendedInterface : IBaseInterface
{
new event EventHandler<ExtendedArgs> Handler;
}
When we try to add an event to the extended eventhandler and fire the event, then eventhandler is never invoked. This can be seen with the following test case, where the event should have set the bool to true when fired, but it fails at the assert.
[TestMethod]
public void TestMethodFail()
{
IExtendedInterface targetInterface = Isolate.Fake.Instance<IExtendedInterface>();
var sentArgs = Isolate.Fake.Instance<ExtendedArgs>();
bool eventWasCalled = false;
EventHandler<ExtendedArgs> testEventHandler = new EventHandler<ExtendedArgs>((sender, args) =>
{
eventWasCalled = true;
});
targetInterface.Handler += testEventHandler;
Isolate.Invoke.Event(() => targetInterface.Handler += null, targetInterface, sentArgs);
Assert.IsTrue(eventWasCalled);
}
We've found that what is likely happening is that the invoker is firing the base eventhandler, as the following test passes
[TestMethod]
public void TestMethodPass()
{
IExtendedInterface targetInterface = Isolate.Fake.Instance<IExtendedInterface>();
var sentArgs = Isolate.Fake.Instance<ExtendedArgs>();
bool eventWasCalled = false;
EventHandler<BaseArgs> testEventHandler = new EventHandler<BaseArgs>((sender, args) =>
{
eventWasCalled = true;
});
((IBaseInterface)targetInterface).Handler += testEventHandler; // Add the event to the base eventhandler instead
Isolate.Invoke.Event(() => targetInterface.Handler += null, targetInterface, sentArgs);
Assert.IsTrue(eventWasCalled);
}