This project is read-only.

fire event let the object sender null

May 9, 2014 at 4:42 PM
I am refering to the NMockTest coming with NMock3:

in the test method, the result of EventBinding is store into an invoker and the binding is indeed done with a event handler method _test_Event(object sender, EventArgs e) .

As expected, when invoker.invoke is called the event handler method is invoked so that we can see that the event is fired.

My point is that the object sender remains null.

any idea?

thanks for your help!
        [TestMethod]
        public void EventTest()
        {
            var mock = _factory.CreateMock<ITest>();
            var invoker = mock.Expects.One.EventBinding(_ => _.Event += null);

            var controller = new Controller(mock.MockObject);
            controller.InitEvents();

            Assert.IsNull(controller.Status);
            invoker.Invoke();
            Assert.AreEqual("Event Fired!", controller.Status);

        }

        public interface ITest
        {
            string Prop { get; set; }
            Version Method(int a, int b, int c, int d);
            string Method(Version version);
            event EventHandler Event;
        }
        public class Controller
        {
            public string Status;
            private ITest _test;
            public Controller(ITest test)
            {
                _test = test;
            }

            public string PropActions(string arg1)
            {
                _test.Prop = arg1;
                return _test.Prop + arg1;
            }
            public Version GetVersion(int a, int b, int c, int d)
            {
                return _test.Method(a, b, c, d);
            }
            public string GetVersion(Version version)
            {
                return _test.Method(version).ToString();
            }
            public void InitEvents()
            {
                _test.Event += _test_Event;
            }

            void _test_Event(object sender, EventArgs e)
            {
                Status = "Event Fired!";
            }
        }
    }
Aug 8, 2014 at 5:36 AM
Hello dadakoko,

The invoker.Invoke() method is overloaded so that you can pass in a sender. Just call invoker.Invoke(sender, null); where sender is your object.