NSubstitute
NSubstitute copied to clipboard
Bugfix/async event handlers return instantly
When trying to test an asynchronous event handler with Raise.Event, the Raise.Event immediately returns on the first thread change in the asynchronous event handler. This makes testing the behaviour of the event handler almost impossible as you cannot be sure that the eventhandler is finished. That leads to bad test code, for example just waiting for a set amount of time in hopes the event handler has finished.
I fixed this bug by waiting for the event handler to finish with GetAwaiter().GetResult(); which is the safest way to synchronously wait for an asnychronous method to finish. As handler.DynamicInvoke(eventArguments) already returned a Task independent of the return value of the event handler, it made this bugfix really small.