Beispiel #1
0
        public void EventStub_WireUnwireTests()
        {
            var eventStub        = new EventStub(typeof(ISampleInterface));
            var simpleEventFired = false;
            var cancelEventFired = false;
            var actionFired      = false;
            var funcFired        = false;

            // add event handlers
            eventStub.AddHandler("SimpleEvent", new EventHandler((sender, args) => simpleEventFired = true));
            eventStub.AddHandler("CancelEvent", new EventHandler <CancelEventArgs>((sender, args) => cancelEventFired = true));
            eventStub.AddHandler("ActionDelegate", new Action(() => actionFired = true));
            eventStub.AddHandler("FuncDelegate", new Func <int, string>(a => { funcFired = true; return(a.ToString()); }));
            eventStub.AddHandler("FuncDelegate", new Func <int, string>(a => { return((a * 2).ToString()); }));

            // wire up events
            var component = new SampleService();

            eventStub.WireTo(component);

            // test if it works
            var result = component.FireHandlers(102030);

            Assert.AreEqual("204060", result);
            Assert.IsTrue(simpleEventFired);
            Assert.IsTrue(cancelEventFired);
            Assert.IsTrue(actionFired);
            Assert.IsTrue(funcFired);

            // unwire
            simpleEventFired = false;
            cancelEventFired = false;
            actionFired      = false;
            funcFired        = false;
            eventStub.UnwireFrom(component);

            // test if it works
            result = component.FireHandlers(123);
            Assert.IsNull(result);
            Assert.IsFalse(simpleEventFired);
            Assert.IsFalse(cancelEventFired);
            Assert.IsFalse(actionFired);
            Assert.IsFalse(funcFired);
        }