Hi!

I want to test whether a particular event of a dependency is fired,
and whether certain methods in the object are called. Both are free to
separate easily, but both does not work in a Test. I get the following
exception when creating the PartialMocks:



Rhino.Mocks.Exceptions.ExpectationViolationException:
ICommunicator.add_IsConnected(System.EventHandler); Expected #1,
Actual #0.
ICommunicator.add_IsConnected(any); Expected #1, Actual #0.



This is my Code and my Test:


    public class Controller : IDisposable
    {
        private readonly ICommunicator communicator;

        public Controller(ICommunicator communicator)
        {
            this.communicator = communicator;

            ConnectToCommunicatorEvents();
        }

        internal void ConnectToCommunicatorEvents()
        {
            communicator.IsConnected +=
Communicator_SendMessageSucceed;
        }

        internal void Communicator_SendMessageSucceed(object sender,
EventArgs e)
        {
            SendBlubberMessage();
        }

        public virtual void SendBlubberMessage()
        {
            communicator.SendMessage("blubber");
        }

        public void Dispose()
        {
            communicator.IsConnected -=
Communicator_SendMessageSucceed;
        }
    }


    public class Communicator : ICommunicator
    {
        private bool isConnected;

        public event EventHandler IsConnected;

        public bool Connect(string server)
        {
            ThrowAnExceptionIfAlreadyConnected();
            //do something
            isConnected = true;
            if (IsConnected != null) IsConnected(this,
EventArgs.Empty);

            return true;
        }

        public bool Disconnect()
        {
            ThrowAnExceptionIfNotConnected();
            //do something
            return true;
        }

        void ICommunicator.SendMessage(string message)
        {
            ThrowAnExceptionIfNotConnected();
            //do something
        }

        public virtual void ThrowAnExceptionIfAlreadyConnected()
        {
            if (isConnected) throw new Exception("already
connected!");
        }

        public virtual void ThrowAnExceptionIfNotConnected()
        {
            if (!isConnected) throw new Exception("not connected!");
        }
    }

        [Test]
        public void
If_Communicator_Event_IsConnected_Was_Fired_Controller_SendBlubberMessage_Is_Called()
        {
            IEventRaiser eventRaiser;
            var mockRepository = new MockRepository();
            var communicatorDynamicMock =
mockRepository.DynamicMock<ICommunicator>();
            var controllerPartialMock =
mockRepository.PartialMock<Controller>(communicatorDynamicMock);

            using (mockRepository.Record())
            {
                communicatorDynamicMock.IsConnected += null;
                eventRaiser =
LastCall.IgnoreArguments().GetEventRaiser();

                controllerPartialMock.SendBlubberMessage();
                LastCall.Callback(new Func<bool>(() => true));
            }

            using (mockRepository.Playback())
            {
                eventRaiser.Raise(null, EventArgs.Empty);
            }
        }



What am I doing wrong?

-- 
You received this message because you are subscribed to the Google Groups 
"Rhino.Mocks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rhinomocks?hl=en.

Reply via email to