Question

I am getting the following error when trieing to raise a Rhino Mock event

Invalid call, the last call has been used or no call has been made (make sure that you are calling a virtual (C#) / Overridable (VB) method)

Here is aminimal example that will compile. I thougth i did everythin correctly.

namespace StackOverFlow
{
    using NUnit.Framework;
    using Rhino.Mocks;
    using Rhino.Mocks.Interfaces;

    public delegate void EventHandler();

    public interface IHasEvent
    {
        event EventHandler InterfaceEvent;
    }

    public class ClassUnderTest
    {
        public ClassUnderTest(IHasEvent hasEvent)
        {
            this.EventCounter = 0;
            hasEvent.InterfaceEvent += this.IncrementCounter;
        }

        public int EventCounter { get; set; }

        private void IncrementCounter()
        {
            ++this.EventCounter;
        }
    }

    [TestFixture]
    public class RhinoMockTest
    {
        [Test]
        public void TestEventRaising()
        {
            IHasEvent mocked = MockRepository.GenerateMock<IHasEvent>();

            mocked.InterfaceEvent += null;
            LastCall.IgnoreArguments(); // <- Exception here
            IEventRaiser raiser = LastCall.GetEventRaiser();

            ClassUnderTest cut = new ClassUnderTest(mocked);
            raiser.Raise();

            Assert.AreEqual(1, cut.EventCounter);
        }
    }
}

I looked into other examples on stackoverflow and the internet. I was unable to apply those solutions. I do not see the error in this code. How can i raise the event from the mock?

Était-ce utile?

La solution

You should try the newer syntax of event raising:

IHasEvent mocked = MockRepository.GenerateMock<IHasEvent>();
ClassUnderTest cut = new ClassUnderTest(mocked);
mocked.Raise(m => m.InterfaceEvent += null);

Assert.AreEqual(1, cut.EventCounter);
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top