Raising events on a mock is easy with Rhino mocks. I wrote a whole article about it.

Way back then I wrote,

If we try to use the same syntax on the parameterless event like so eventraiser.GetEventRaiser(e => e.event1 += null).Raise(null); we get an InvalidOperationException (You have called the event raiser with the wrong number of parameters. Expected 0 but was 0). Expected 0 but was 0 doesn’t sound completely right but I get the picture. If you try to raise an event on a parameterd event like you would on a parameterless event, like so eventraiser.Raise(e => e.event2 += null); then you get this InvalidOperationException (You have called the event raiser with the wrong number of parameters. Expected 1 but was 0) where the 1 is the number of parameters you should have.

And I forgot that sometimes you do want to pass null as an argument to check if that raises and exception or to check if your code handles that situation like you intended.

So I have some code that raises an event that takes an object as a parameter. Something like this.

using System;
using Rhino.Mocks;
using NUnit.Framework;
 
namespace TestingRhinoMocks
{
    public delegate void MyDelegate(Object test);
    
    public interface IEventsRaiser
    {
        event MyDelegate event;
    }
 
    public class EventRaiser : IEventsRaiser
    {
        public event MyDelegate event;
    }
 
    public class EventConsumer
    {
        private IEventsRaiser eventraiser;
 
        public EventConsumer(IEventsRaiser eventraiser)
        {
            this.eventraiser = eventraiser;
            eventraiser.event1 += Eventhandler1;
        }
 
        public Object Message { get; set; }
 
        public void Eventhandler(Object test)
        {
            Message = test;
        }
   
    }

So when I do this.

/// <summary>
        /// RaiseEventWithNullPassedIn
        /// </summary>
        [Test]
        public void RaiseEventWithNullPassedIn()
        {
            var mocks = new StructureMap.AutoMocking.RhinoAutoMocker<EventConsumer>();
            var e1 = mocks.ClassUnderTest;
            var e2 = mocks.Get<IEventsRaiser >();
            facade.GetEventRaiser(x => x.event += null).Raise(null);
            Assert.IsNull(e1.Message);
        }

I will get the exception.

But when I do this the test passes.

/// <summary>
        /// RaiseEventWithNullPassedIn
        /// </summary>
        [Test]
        public void RaiseEventWithNullPassedIn()
        {
            var mocks = new StructureMap.AutoMocking.RhinoAutoMocker<EventConsumer>();
            var e1 = mocks.ClassUnderTest;
            var e2 = mocks.Get<IEventsRaiser >();
            Object o = null;
            facade.GetEventRaiser(x => x.event += null).Raise(o);
            Assert.IsNull(e1.Message);
        }

Did you see the subtle difference? Very subtle indeed.