【问题标题】:RhinoMocks - Raising event on mocked interface failsRhinoMocks - 在模拟界面上引发事件失败
【发布时间】:2013-09-05 17:42:16
【问题描述】:

我在尝试引发 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 和互联网的其他示例。 我无法应用这些解决方案。 我没有看到此代码中的错误。 如何从模拟中引发事件?

【问题讨论】:

    标签: c# unit-testing events event-handling rhino-mocks


    【解决方案1】:

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

    IHasEvent mocked = MockRepository.GenerateMock<IHasEvent>();
    ClassUnderTest cut = new ClassUnderTest(mocked);
    mocked.Raise(m => m.InterfaceEvent += null);
    
    Assert.AreEqual(1, cut.EventCounter);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多