-
-
Notifications
You must be signed in to change notification settings - Fork 802
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
"Expression is not an event add" when using .Raises() #1175
Comments
When DoSomething is called the corresponding
There is similar code hit from SetupAdd which should add the handler to be called in Mock.RaiseEvent
if we look at the events on the proxy ( note that I have pasted your code into Moq tests )
Which disagrees with the single event declared on the
I agree that it is a mess of inheritance and new versions of events but it still looks like a bug to me ( in Castle.Core.) @stakx Your thoughts |
@howcheng, thanks for reporting this issue. And thanks @tonyhallett for doing some preliminary investigating. First, to simplify things a little, let's use a more minimal repro (derived from the original one above mostly through a process of elimination): using System;
using Moq;
Mock<IDerived> mock = new Mock<IDerived>();
mock.Setup(x => x.RaiseEvent()).Raises(x => x.Event += null, false);
mock.Object.Event += _ => { Console.WriteLine("Hello world!!!"); };
mock.Object.RaiseEvent();
public interface IBase
{
event Action Event;
void RaiseEvent();
}
public interface IDerived : IBase
{
new event Action<bool> Event;
} I've played around with this a bit, and the problem appears to be triggered by having two events of the same name but different signature, spread across two types which are related via subtyping. I've taken a closer look at what's going on under the hood. Among other things, I've used a modified version of Moq that stores DynamicProxy's dynamic assembly to disk, then I inspected that assembly using
This kind of overloading is illegal, as
This event duplication then causes Reflection to report incorrect results, making the following ... which in turn causes the error message initially reported. We'll likely need to get this fixed in DynamicProxy first before we can do anything in Moq (if any change will be necessary after patching DynamicProxy). |
Here is another instance of Raise() failing when the base class event is hidden by a new generic child class event.
|
@toppiovi, FYI, your test case has a slight problem that would prevent it from passing even if there were no issue with Moq: -aEventsMock.Raise(theO => theO.Created += null, true);
+aEventsMock.Raise(theO => theO.Created += null, /* sender */, true); I'm going to add a corrected version of your test case to the regression suite. |
I've just pushed Moq version 4.18.2 to NuGet, that version includes a bugfix for this issue. Thanks again for reporting! |
When trying to have a mocked method raise an event, I'm getting the error message "Expression is not an event add". The interface I'm using is a mess of inheritance and new versions of events, so maybe that's related, but here's a fiddle with a stripped-down version where you can see the error happening.
https://dotnetfiddle.net/kqhOln
Did I do something wrong, or is this a bug?
Thanks.
The text was updated successfully, but these errors were encountered: