Provide a way to pass null for custom event args so that Raise.EventWith can be used to raise events for custom event args without a default constructor #788
Labels
feature-request
Request for a new NSubstitute feature
Is your feature request related to a problem? Please describe.
Currently if we want to test raising an event that has custom events args (rather than just using EventArgs), the custom event args class must have a default constructor or some method to create the object. This is because NSubstitute will attempt to create an instance of the event args if we provide null (see code here) when calling Raise.EventWith.
However, it is not necessary for a class to have a public constructor (e.g. constructor could be internal), and therefore if the custom event args class doesn't have a publicly accessible constructor, there is no way to raise the event using Raise.EventWith
Describe the solution you'd like
It would be great if there was a way that Raise.EventWith could be called where null can be provided for the custom event arg, and NSubstitue does not attempt to create an instance of the custom event arg.
One possible solution is to not throw the exception if the call to GetDefaultConstructor returns null, and in that case, just return null from the GetDefaultForEventArgType method.
Additional context
Below is some example code demonstrating this problem:
And the unit test below will fail with the message: NSubstitute.Exceptions.CannotCreateEventArgsException : Cannot create CustomEventArgs for this event as it has no default constructor. Provide arguments for this event by calling Raise.EventWith(CustomEventArgs).
The text was updated successfully, but these errors were encountered: