其他分享
首页 > 其他分享> > CodeGo.net> RhinoMocks-模拟接口上引发事件失败

CodeGo.net> RhinoMocks-模拟接口上引发事件失败

作者:互联网

尝试引发Rhino Mock事件时出现以下错误

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)

这是一个可以编译的最小示例.我想我做的一切都正确.

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);
        }
    }
}

我研究了关于stackoverflow和Internet的其他示例.
我无法应用这些解决方案.
在此代码中看不到错误.
我如何从模拟中引发事件?

解决方法:

您应该尝试使用新的事件引发语法:

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

Assert.AreEqual(1, cut.EventCounter);

标签:unit-testing,event-handling,events,c,rhino-mocks
来源: https://codeday.me/bug/20191030/1968031.html