Skip to content
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

Implement raising of non-virtual events via Mock<T>.Raise #457

Closed
stakx opened this issue Sep 25, 2017 · 2 comments
Closed

Implement raising of non-virtual events via Mock<T>.Raise #457

stakx opened this issue Sep 25, 2017 · 2 comments

Comments

@stakx
Copy link
Contributor

stakx commented Sep 25, 2017

This unit test in UnitTests/MockedEventsFixture.cs has been disabled forever, but its presence suggests that Moq was at some point in the past supposed to be able to raise non-virtual events via Mock<T>.Raise.:

[Fact(Skip = "Events on non-virtual events not supported yet")]
public void EventRaisingFailsOnNonVirtualEvent()
{
   var mock = new Mock<WithEvent>();
   var raised = false;
   mock.Object.ClassEvent += delegate { raised = true; };
   mock.Raise(x => x.ClassEvent += null, EventArgs.Empty);
   Assert.True(raised);
}
@stakx
Copy link
Contributor Author

stakx commented Nov 18, 2017

This cannot be implemented in a straightforward manner because expression trees do not allow assignment operators such as += or -=. The delegate passed to Raise would either have to be decompiled to retrieve such an expression (which in turn requires .NET Standard 2.0 while we're still at 1.3) or a major API change (e.g. using nameof instead of a lambda).

Neither alternative seems a particularly good fit for Moq 4 so this is perhaps better left unimplemented for the time being. Once Moq targets .NET Standard 2.0, or C# expression lambdas become less restrictive regarding assignments, we can consider doing this once again.

@stakx
Copy link
Contributor Author

stakx commented Sep 5, 2019

Moq has since gotten a new delegate reconstruction algorithm, which however still isn't based on IL decompilation as I assumed (above) it might be. (This is what would have required .NET Standard 2.0.) So we still can't support this scenario for the forseeable future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant