public void RaiseEvent()
        {
            IWithEvents eventHolder = MockRepository.Mock <IWithEvents>();

            eventHolder.SetUnexpectedBehavior(UnexpectedCallBehaviors.BaseOrDefault);

            eventHolder.ExpectEvent(x => x.Blah += null)
            .IgnoreArguments();

            eventHolder.Expect(x => x.RaiseEvent())
            .DoInstead(new System.Threading.ThreadStart(delegate()
            {
                eventHolder.Raise(x => x.Blah += null,
                                  new object[] { this, EventArgs.Empty });
            }));

            IEventSubscriber eventSubscriber = MockRepository.Mock <IEventSubscriber>();

            eventSubscriber.SetUnexpectedBehavior(UnexpectedCallBehaviors.BaseOrDefault);
            eventSubscriber.Expect(x => x.Hanlder(this, EventArgs.Empty));

            eventHolder.Blah += new EventHandler(eventSubscriber.Hanlder);
            eventHolder.RaiseEvent();

            eventHolder.VerifyExpectations();
            eventSubscriber.VerifyExpectations();
        }
        public void VerifyingThatEventWasAttached()
        {
            IWithEvents events = MockRepository.Mock <IWithEvents>();

            events.SetUnexpectedBehavior(UnexpectedCallBehaviors.BaseOrDefault);
            events.ExpectEvent(x => x.Blah += new EventHandler(events_Blah));

            MethodThatSubscribeToEventBlah(events);

            events.VerifyExpectations();
        }
        public void VerifyingExceptionIfEventIsNotAttached()
        {
            IWithEvents events = MockRepository.Mock <IWithEvents>();

            events.SetUnexpectedBehavior(UnexpectedCallBehaviors.BaseOrDefault);

            events.ExpectEvent(x => x.Blah += new EventHandler(events_Blah));

            Assert.Throws <ExpectationViolationException>(
                () => events.VerifyExpectations());
        }
        public void VerifyingThatCanAttackOtherEvent()
        {
            IWithEvents events = MockRepository.Mock <IWithEvents>();

            events.SetUnexpectedBehavior(UnexpectedCallBehaviors.BaseOrDefault);

            events.ExpectEvent(x => x.Blah += new EventHandler(events_Blah))
            .IgnoreArguments();

            events.Blah += new EventHandler(events_Blah_Other);

            events.VerifyExpectations();
        }
        public void BetterErrorMessageOnIncorrectParametersCount()
        {
            IWithEvents events = MockRepository.Mock <IWithEvents>();

            events.SetUnexpectedBehavior(UnexpectedCallBehaviors.BaseOrDefault);

            events.ExpectEvent(x => x.Blah += null)
            .IgnoreArguments();

            events.Blah += delegate { };

            Assert.Throws <InvalidOperationException>(
                () => events.Raise(x => x.Blah += null, new object[0]));
        }
        public void CanSetExpectationToUnsubscribeFromEvent()
        {
            IWithEvents events = MockRepository.Mock <IWithEvents>();

            events.SetUnexpectedBehavior(UnexpectedCallBehaviors.BaseOrDefault);

            events.ExpectEvent(x => x.Blah += new EventHandler(events_Blah));
            events.ExpectEvent(x => x.Blah -= new EventHandler(events_Blah));


            events.Blah += new EventHandler(events_Blah);
            events.Blah -= new EventHandler(events_Blah);

            events.VerifyExpectations();
        }
        public void RaiseEventUsingExtensionMethod()
        {
            IWithEvents eventHolder = MockRepository.Mock <IWithEvents>();

            eventHolder.SetUnexpectedBehavior(UnexpectedCallBehaviors.BaseOrDefault);

            bool called = false;

            eventHolder.Blah += delegate
            {
                called = true;
            };

            eventHolder.Raise(stub => stub.Blah += null, EventArgs.Empty);

            Assert.True(called);
        }
        public void UsingEventRaiserFromExtensionMethod()
        {
            IWithEvents eventHolder = MockRepository.Mock <IWithEvents>();

            eventHolder.SetUnexpectedBehavior(UnexpectedCallBehaviors.BaseOrDefault);

            eventHolder.StubEvent(x => x.Blah += null);

            bool called = false;

            eventHolder.Blah += delegate
            {
                called = true;
            };

            eventHolder.Raise(x => x.Blah += null, EventArgs.Empty);

            Assert.True(called);
            eventHolder.VerifyAllExpectations();
        }