gpt4 book ai didi

c# - RhinoMocks - 在模拟界面上引发事件失败

转载 作者:行者123 更新时间:2023-11-30 15:32:37 26 4
gpt4 key购买 nike

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

最佳答案

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

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

Assert.AreEqual(1, cut.EventCounter);

关于c# - RhinoMocks - 在模拟界面上引发事件失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18642685/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com