3

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?

4

1 回答 1

4

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);
于 2013-09-05T20:41:03.197 回答