验证 Moq 与 Rhino Mocks 中是否存在事件处理程序

发布于 2025-01-07 21:51:41 字数 695 浏览 0 评论 0 原文

我希望能够测试对象是否已订阅事件。在Rhino Mocks中,下面的代码验证了这一点,但是我找不到在Moq中做到这一点的方法。

public class Presenter
{
    IView view;
    public Presenter(IView view)
    {
        this.view = view;
        this.view.Load += new EventHandler(view_Load);
    }
    void view_Load(object sender, EventArgs e)
    {
        throw new Exception("Not implemented.");
    }
}


[Test]
public void VerifyAttachesToViewEvents()
{
    MockRepository mocks = new MockRepository();
    IView viewMock = (IView)mocks.CreateMock(typeof(IView));
    using (mocks.Record())
    {
        viewMock.Load += null;
        LastCall.IgnoreArguments();
    }
    new Presenter(viewMock);
    mocks.VerifyAll();
}

I want to be able to test whether a object has subscribed to an event. In Rhino Mocks the below code verifies this however I cannot find a way to do it in Moq.

public class Presenter
{
    IView view;
    public Presenter(IView view)
    {
        this.view = view;
        this.view.Load += new EventHandler(view_Load);
    }
    void view_Load(object sender, EventArgs e)
    {
        throw new Exception("Not implemented.");
    }
}


[Test]
public void VerifyAttachesToViewEvents()
{
    MockRepository mocks = new MockRepository();
    IView viewMock = (IView)mocks.CreateMock(typeof(IView));
    using (mocks.Record())
    {
        viewMock.Load += null;
        LastCall.IgnoreArguments();
    }
    new Presenter(viewMock);
    mocks.VerifyAll();
}

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

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

发布评论

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

评论(2

短叹 2025-01-14 21:51:41

我会引发该事件并验证是否引发了异常。无论如何,您可能需要检查 SUT 中的某些逻辑是否已执行,而不仅仅是订阅了事件:

[Test]
public void VerifyAttachesToViewEvents()
{
    // arrange
    var mock = new Mock<IView>();
    new Presenter(mock.Object);

    // act
    Action action = () => mock.Raise(view => view.Load += null, EventArgs.Empty);

    // assert
    action.ShouldThrow<Exception>()
        .WithMessage("Not implemented.");
}

我不确定您使用的是什么单元测试框架,因此利用了 断言部分的流畅断言。如果您使用 NUnit,代码可能是:

[Test]
public void VerifyAttachesToViewEvents()
{
    // arrange
    var mock = new Mock<IView>();
    new Presenter(mock.Object);

    // act & assert
    Assert.Throws<Exception>(() => 
         mock.Raise(view => view.Load += null, EventArgs.Empty));
}

I would raise the event and verify whether an exception has been thrown. Anyway you probably need to check if some logic in your SUT is executed not just that an event has been subscribed to:

[Test]
public void VerifyAttachesToViewEvents()
{
    // arrange
    var mock = new Mock<IView>();
    new Presenter(mock.Object);

    // act
    Action action = () => mock.Raise(view => view.Load += null, EventArgs.Empty);

    // assert
    action.ShouldThrow<Exception>()
        .WithMessage("Not implemented.");
}

I was not sure what unit testing framework you use, so leveraged fluent assertions in the assert part. If you use NUnit the code may be:

[Test]
public void VerifyAttachesToViewEvents()
{
    // arrange
    var mock = new Mock<IView>();
    new Presenter(mock.Object);

    // act & assert
    Assert.Throws<Exception>(() => 
         mock.Raise(view => view.Load += null, EventArgs.Empty));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文