我在尝试引发 Rhino 模拟事件时收到以下错误
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和互联网的其他例子。我无法应用这些解决方案。我在此代码中没有看到错误。如何从模拟中引发事件?
您应该尝试较新的事件引发语法:
IHasEvent mocked = MockRepository.GenerateMock<IHasEvent>();
ClassUnderTest cut = new ClassUnderTest(mocked);
mocked.Raise(m => m.InterfaceEvent += null);
Assert.AreEqual(1, cut.EventCounter);