Skip to content

Adding null event handler to substituted event leads to exception in Raise.EventWith. #647

@pappe82

Description

@pappe82

A "normal" event on an instance ignores null subscriptions. If I do so on a substituted interface, a null subscription leads to an exception. I have a not so nice legacy code with different listeners to an event an one class that does null-attach internally under some conditions, would be nice if this behaviour would be able to be tested as well. Example code for the problem:

        public interface IEventSource
        {
            event EventHandler<EventArgs> MyEvent;
        }

        [TestMethod]
        public void TestEvent()
        {
            var raised = false;
            var source = Substitute.For<IEventSource>();
            source.MyEvent += null; //<- this leads to fail on Raise
            source.MyEvent += (s, e) => raised = true;
            source.MyEvent += Raise.EventWith(new EventArgs());

            Assert.IsTrue(raised);

        }´´´

Metadata

Metadata

Assignees

Labels

bugReported problem with NSubstitute behaviourgood first issueFor smaller tasks that do not require in-depth knowledge of the code base

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions