We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
Hello there!
It seems that when an exception is raised inside an event raise, it gets caught by Moq and it goes undetected. Might have to do with it being async?
Example:
internal class Program { static async Task Main(string[] args) { var eventManager = new EventManager(); var eventConsumer = new EventConsumer(eventManager); await eventManager.OnEventHappened(1000); Console.WriteLine("Unreacheble, exception thrown"); } } public delegate Task CustomEvent(int number); public interface IEventManager { event CustomEvent CustomEvent; } public class EventConsumer { private readonly IEventManager _eventManager; public EventConsumer(IEventManager eventManager) { _eventManager = eventManager; _eventManager.CustomEvent += _eventManager_CustomEvent; } private async Task _eventManager_CustomEvent(int number) { await Task.Delay(number); throw new NotImplementedException(); } } public class EventManager : IEventManager { public event CustomEvent CustomEvent; public async Task OnEventHappened(int number) { await CustomEvent.Invoke(number); } }
Failing tests:
[Test] public void ShouldThrowException() { var eventManagerMock = new Mock<IEventManager>(MockBehavior.Strict); var consumer = new EventConsumer(eventManagerMock.Object); Assert.Throws<NotImplementedException>(() => eventManagerMock.Raise(x => x.CustomEvent += null, 1000)); } [Test] public void ShouldThrowException2() { var eventManagerMock = new Mock<IEventManager>(MockBehavior.Strict); var consumer = new EventConsumer(eventManagerMock.Object); eventManagerMock.Setup(x => x.OnEventHappened(1000)).Returns(Task.CompletedTask).Raises(x => x.CustomEvent += null, 1000); Assert.Throws<NotImplementedException>(() => eventManagerMock.Object.OnEventHappened(1000)); }
Am I doing something wrong or is it a bug indeed?
Thanks!
The text was updated successfully, but these errors were encountered:
Now I see https://github.com/moq/moq/pull/1313 and https://github.com/moq/moq/issues/977, sorry about the duplicate.
Sorry, something went wrong.
So that leaves another question, any updates on 4.19? I see the milestone is closed.
Tested with 4.20-beta, RaiseAsync and ThrowsAsync working together like a charm.
No branches or pull requests
Hello there!
It seems that when an exception is raised inside an event raise, it gets caught by Moq and it goes undetected. Might have to do with it being async?
Example:
Failing tests:
Am I doing something wrong or is it a bug indeed?
Thanks!
The text was updated successfully, but these errors were encountered: