Ejemplo n.º 1
0
        public void EventStubHandlerCountTests()
        {
            var eventStub     = new EventStub(typeof(ISampleInterface));
            var sampleService = new SampleService();

            eventStub.WireTo(sampleService);
            Assert.AreEqual(0, sampleService.SimpleEventHandlerCount);

            eventStub.AddHandler("SimpleEvent", new EventHandler((s, e) => { }));
            Assert.AreEqual(1, sampleService.SimpleEventHandlerCount);

            eventStub.AddHandler("SimpleEvent", new EventHandler((s, e) => { }));
            Assert.AreEqual(2, sampleService.SimpleEventHandlerCount);
        }
Ejemplo n.º 2
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);
        }