如何在 Rhino Mocks 中的严格模拟中引发事件?

发布于 2024-10-25 09:41:16 字数 1517 浏览 1 评论 0原文

我想测试以下代码在引发 MyEvent 事件时是否设置 Raising 属性:

public interface IEventRaiser
{
    event EventHandler<Args> MyEvent;
    void DoSomething();
}

public class EventSubscriber
{
    public EventSubscriber(IEventRaiser raiser)
    {
        this.raiser = raiser;
        this.raiser.MyEvent += EventRaised;
    }

    private readonly IEventRaiser raiser;

    private void EventRaised(object sender, Args args)
    {
        raiser.DoSomething();
        Raised = true;
    }

    public bool Raised {get; private set;}
}

我正在使用以下 NUnit 测试代码,但该事件似乎从未引发。 EventRaished 方法永远不会运行:

[TestFixture]
public class EventSubscriberTests
{
    private MockRepository mocks;
    private IEventRaiser raiser;
    private EventSubscriber target;

    [SetUp]
    public void SetUp()
    {
        mocks = new MockRepository();
        raiser = mocks.StrictMock<IEventRaiser>();
        raiser.Expect(r => r.MyEvent += null).IgnoreArguments();
        mocks.ReplayAll();

        target = new EventSubscriber(raiser);
    }

    [TearDown]
    public void TearDown()
    {
        mocks.VerifyAll();
    }

    [Test]
    public void VerifyEventIsSubscribedTo()
    {
        raiser.BackToRecord();
        raiser.Expect(r => r.DoSomething());
        raiser.Replay();

        raiser.Raise(r => r.MyEvent += null, raiser, new Args {Property = true} );

        Assert.IsTrue(target.Raised);
    }
}

如果我在引发事件之前不添加额外的期望,则效果很好。但是,我似乎无法让它与它一起工作。我做错了什么?

I want to test that the following code sets the Raised property when the MyEvent event is raised:

public interface IEventRaiser
{
    event EventHandler<Args> MyEvent;
    void DoSomething();
}

public class EventSubscriber
{
    public EventSubscriber(IEventRaiser raiser)
    {
        this.raiser = raiser;
        this.raiser.MyEvent += EventRaised;
    }

    private readonly IEventRaiser raiser;

    private void EventRaised(object sender, Args args)
    {
        raiser.DoSomething();
        Raised = true;
    }

    public bool Raised {get; private set;}
}

I am using the following NUnit test code, but the event never seems to be raised. The EventRaised method is never running:

[TestFixture]
public class EventSubscriberTests
{
    private MockRepository mocks;
    private IEventRaiser raiser;
    private EventSubscriber target;

    [SetUp]
    public void SetUp()
    {
        mocks = new MockRepository();
        raiser = mocks.StrictMock<IEventRaiser>();
        raiser.Expect(r => r.MyEvent += null).IgnoreArguments();
        mocks.ReplayAll();

        target = new EventSubscriber(raiser);
    }

    [TearDown]
    public void TearDown()
    {
        mocks.VerifyAll();
    }

    [Test]
    public void VerifyEventIsSubscribedTo()
    {
        raiser.BackToRecord();
        raiser.Expect(r => r.DoSomething());
        raiser.Replay();

        raiser.Raise(r => r.MyEvent += null, raiser, new Args {Property = true} );

        Assert.IsTrue(target.Raised);
    }
}

If I don't add the extra expectation in before raising the event, this works fine. However, I can't seem to make it work with it. What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

长发绾君心 2024-11-01 09:41:16

BackToRecord 方法默认清除所有先前的期望。我实际上需要使用 BackToRecord(BackToRecordOptions.None) 来保持以前的状态。

The BackToRecord method clears all previous expectations by default. I actually needed to use BackToRecord(BackToRecordOptions.None) in order to maintain the previous state.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文