Skip to content

Commit

Permalink
Merge pull request #1276 from stakx/redeclared-events
Browse files Browse the repository at this point in the history
Add regression tests for subscribing to & raising redeclared event
  • Loading branch information
stakx committed Aug 2, 2022
2 parents 4ab6ce1 + e52a695 commit a54d18b
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -13,6 +13,7 @@ The format is loosely based on [Keep a Changelog](http://keepachangelog.com/en/1

#### Fixed

* "Expression is not an event add" when using `.Raises()` with redeclared event (@howcheng, #1175)
* Throws `TypeLoadException` on mock when a record has a base record on .NET 6 (@tgrieger-sf, #1273)


Expand Down
71 changes: 71 additions & 0 deletions tests/Moq.Tests/Regressions/IssueReportsFixture.cs
Expand Up @@ -3688,6 +3688,77 @@ public virtual void SecondCall()

#endregion

#region 1175

public class Issue1175
{
[Fact]
public void Can_subscribe_to_and_raise_redeclared_event_1()
{
var handled = false;

var mock = new Mock<IDerived>();
mock.Setup(x => x.RaiseEvent()).Raises(x => x.Event += null, false);
mock.Object.Event += _ => handled = true;

mock.Object.RaiseEvent();

Assert.True(handled);
}

public interface IBase
{
event Action Event;
void RaiseEvent();
}

public interface IDerived : IBase
{
new event Action<bool> Event;
}

[Fact]
public void Can_subscribe_to_and_raise_redeclared_event_2()
{
var aEventsMock = new Mock<IGenericHidingEvents<bool>>();
var aConsumer = new GenericHidingEventConsumer(aEventsMock.Object);

aEventsMock.Raise(theO => theO.Created += null, this, true);

Assert.True(aConsumer.EventHandled);
}

public interface IEvents
{
event EventHandler Created;
}

public interface IGenericHidingEvents<T> : IEvents
{
new event EventHandler<T> Created;
}

public class GenericHidingEventConsumer
{
private IGenericHidingEvents<bool> myHidingEvents;

public GenericHidingEventConsumer(IGenericHidingEvents<bool> theHidingEvents)
{
this.myHidingEvents = theHidingEvents;
this.myHidingEvents.Created += this.HidingEventsOnCreated;
}

private void HidingEventsOnCreated(object theSender, bool theE)
{
EventHandled = true;
}

public bool EventHandled { get; set; }
}
}

#endregion

#region 1217

public class Issue1217
Expand Down

0 comments on commit a54d18b

Please sign in to comment.