Beispiel #1
0
        public void UsingLambdaExpressionsToRegisterEventHandlers()
        {
            var classWithEvents = new ClassWithEvents();
            EventHandler <EventArgs> someEvent = ((sender, e) => Log("SomeEvent", sender, e));

            classWithEvents.SomeEvent += someEvent;

            classWithEvents.OnSomeEvent(EventArgs.Empty);
        }
Beispiel #2
0
        public void UsingLambdaExpressionsToRegisterEventHandlers_LongHand()
        {
            var classWithEvents = new ClassWithEvents();
            Expression <EventHandler <EventArgs> > expression = (sender, e) => Log("SomeEvent", sender, e);
            EventHandler <EventArgs> someEvent = expression.Compile();

            classWithEvents.SomeEvent += someEvent;

            classWithEvents.OnSomeEvent(EventArgs.Empty);
        }
Beispiel #3
0
        public void EventInvocationInThirdPartyClassShouldSuccess()
        {
            var e       = new ClassWithEvents();
            var counter = 0;

            e.OnString += (sender, s) => counter++;
            e.OnString += (sender, s) => counter++;

            EventUtils.InvokeAppropriateEvent(e, string.Empty);

            //Unfortunately FluentAssertions MonitorEvent/ShouldRaise Methods make this test fail
            counter.Should().Be(2);
        }
Beispiel #4
0
        public void ProxyInterceptsEvents()
        {
            IInstanceInterceptor          interceptor          = new InterfaceInterceptor();
            ClassWithEvents               target               = new ClassWithEvents();
            IInterceptingProxy            proxy                = interceptor.CreateProxy(typeof(IDoEvents), target);
            CallCountInterceptionBehavior interceptionBehavior = new CallCountInterceptionBehavior();

            proxy.AddInterceptionBehavior(interceptionBehavior);

            ((IDoEvents)proxy).SomeEvent += (s, a) => { };

            Assert.AreEqual(1, interceptionBehavior.CallCount);
        }
Beispiel #5
0
        public void Raise_EventWithReturnValue_ReturnsValueFromSubscriber()
        {
            Smock.Run(context =>
            {
                ClassWithEvents first       = new ClassWithEvents();
                first.EventWithReturnValue += value => value.ToString();

                object result = context.Raise(() => first.EventWithReturnValue += null,
                                              () => first.EventWithReturnValue -= null, 42);

                Assert.AreEqual("42", result);
            });
        }
Beispiel #6
0
        public void Raise_NonMatchingTargets_ThrowsException()
        {
            Assert.Throws <ArgumentException>(() =>
            {
                Smock.Run(context =>
                {
                    var first  = new ClassWithEvents();
                    var second = new ClassWithEvents();

                    context.Raise(() => first.TheEvent += null, () => second.TheEvent -= null, default(EventArgs));
                });
            });
        }
Beispiel #7
0
        public void Raise_StaticEvent_OriginalEventInvokesSubscribers()
        {
            int invocationCount = 0;

            Smock.Run(context =>
            {
                ClassWithEvents.StaticEvent += (sender, args) => ++ invocationCount;
                ClassWithEvents.RaiseStaticEvent(EventArgs.Empty);
                context.Raise(() => ClassWithEvents.StaticEvent += null, () => ClassWithEvents.StaticEvent -= null, default(EventArgs));
            });

            Assert.AreEqual(2, invocationCount);
        }
Beispiel #8
0
        public void Raise_NonMatchingTargets_ThrowsException()
        {
            Assert.Throws<ArgumentException>(() =>
            {
                Smock.Run(context =>
                {
                    var first = new ClassWithEvents();
                    var second = new ClassWithEvents();

                    context.Raise(() => first.TheEvent += null, () => second.TheEvent -= null, default(EventArgs));
                });
            });
        }
Beispiel #9
0
        public void Raise_EventWithReturnValue_ReturnsValueFromSubscriber()
        {
            Smock.Run(context =>
            {
                ClassWithEvents first = new ClassWithEvents();
                first.EventWithReturnValue += (value) => value.ToString();

                object result = context.Raise(() => first.EventWithReturnValue += null,
                    () => first.EventWithReturnValue -= null, 42);

                Assert.AreEqual("42", result);
            });
        }
Beispiel #10
0
        public void Raise_NonStaticEvent_OriginalEventInvokesSubscribers()
        {
            int invocationCount = 0;

            Smock.Run(context =>
            {
                ClassWithEvents instance = new ClassWithEvents();

                instance.TheEvent += (sender, args) => ++ invocationCount;

                instance.RaiseTheEvent(EventArgs.Empty);
                context.Raise(() => instance.TheEvent += null, () => instance.TheEvent -= null, EventArgs.Empty);
            });

            Assert.AreEqual(2, invocationCount);
        }
Beispiel #11
0
        public void Raise_NonStaticEvent_OriginalEventInvokesSubscribers()
        {
            int invocationCount = 0;

            Smock.Run(context =>
            {
                ClassWithEvents instance = new ClassWithEvents();

                instance.TheEvent += (sender, args) => ++invocationCount;

                instance.RaiseTheEvent(EventArgs.Empty);
                context.Raise(() => instance.TheEvent += null, () => instance.TheEvent -= null, EventArgs.Empty);
            });

            Assert.AreEqual(2, invocationCount);
        }
Beispiel #12
0
        public void Raise_NonStaticEvent_PassesEventArgsAndSender()
        {
            Smock.Run(context =>
            {
                ClassWithEvents instance = new ClassWithEvents();

                EventArgs eventArgs = new EventArgs();

                EventArgs passedEventArgs = null;
                object passedSender       = null;

                instance.TheEvent += (sender, args) => { passedEventArgs = args; passedSender = sender; };
                context.Raise(() => instance.TheEvent += null, () => instance.TheEvent -= null, eventArgs);

                Assert.AreSame(instance, passedSender);
                Assert.AreSame(eventArgs, passedEventArgs);
            });
        }
Beispiel #13
0
        public void Raise_UnsubcribeFromNonStaticEvent_DoesntInvokeUnsubscribedHandlerOnRaise()
        {
            bool invoked = false;

            Smock.Run(context =>
            {
                var instance = new ClassWithEvents();

                EventHandler handler = (sender, args) => invoked = true;

                instance.TheEvent += handler;
                instance.TheEvent -= handler;

                context.Raise(() => instance.TheEvent += null, () => instance.TheEvent -= null, default(EventArgs));
            });

            Assert.IsFalse(invoked);
        }
Beispiel #14
0
        public void Raise_NonStaticEvent_RaisesInvokedEventOnCorrectInstance()
        {
            Smock.Run(context =>
            {
                ClassWithEvents first  = new ClassWithEvents();
                ClassWithEvents second = new ClassWithEvents();

                bool firstInvoked = false;
                first.TheEvent   += (sender, args) => firstInvoked = true;

                bool secondInvoked = false;
                second.TheEvent   += (sender, args) => secondInvoked = true;

                context.Raise(() => first.TheEvent += null, () => first.TheEvent -= null, EventArgs.Empty);

                Assert.IsFalse(secondInvoked);
                Assert.IsTrue(firstInvoked);
            });
        }
Beispiel #15
0
        public void ProxySendsOriginalWhenRaisingEvent()
        {
            // arrange
            IInstanceInterceptor          interceptor          = new InterfaceInterceptor();
            ClassWithEvents               target               = new ClassWithEvents();
            IInterceptingProxy            proxy                = interceptor.CreateProxy(typeof(IDoEvents), target);
            CallCountInterceptionBehavior interceptionBehavior = new CallCountInterceptionBehavior();

            proxy.AddInterceptionBehavior(interceptionBehavior);
            object sender = null;

            ((IDoEvents)proxy).SomeEvent += (s, a) => { sender = s; };

            // act
            ((IDoEvents)proxy).TriggerIt();

            // assert
            Assert.AreSame(target, sender);
            Assert.AreEqual(2, interceptionBehavior.CallCount);  // adding + calling TriggerIt
        }
Beispiel #16
0
        public void Raise_NonStaticEvent_PassesEventArgsAndSender()
        {
            Smock.Run(context =>
            {
                ClassWithEvents instance = new ClassWithEvents();

                EventArgs eventArgs = new EventArgs();

                EventArgs passedEventArgs = null;
                object passedSender = null;

                instance.TheEvent += (sender, args) => { passedEventArgs = args; passedSender = sender; };
                context.Raise(() => instance.TheEvent += null, () => instance.TheEvent -= null, eventArgs);

                Assert.AreSame(instance, passedSender);
                Assert.AreSame(eventArgs, passedEventArgs);
            });
        }
Beispiel #17
0
        public void Raise_UnsubcribeFromNonStaticEvent_DoesntInvokeUnsubscribedHandlerOnRaise()
        {
            bool invoked = false;

            Smock.Run(context =>
            {
                var instance = new ClassWithEvents();

                EventHandler handler = (sender, args) => invoked = true;

                instance.TheEvent += handler;
                instance.TheEvent -= handler;

                context.Raise(() => instance.TheEvent += null, () => instance.TheEvent -= null, default(EventArgs));
            });

            Assert.IsFalse(invoked);
        }
Beispiel #18
0
        public void Raise_NonStaticEvent_RaisesInvokedEventOnCorrectInstance()
        {
            Smock.Run(context =>
            {
                ClassWithEvents first = new ClassWithEvents();
                ClassWithEvents second = new ClassWithEvents();

                bool firstInvoked = false;
                first.TheEvent += (sender, args) => firstInvoked = true;

                bool secondInvoked = false;
                second.TheEvent += (sender, args) => secondInvoked = true;

                context.Raise(() => first.TheEvent += null, () => first.TheEvent -= null, EventArgs.Empty);

                Assert.IsFalse(secondInvoked);
                Assert.IsTrue(firstInvoked);
            });
        }