Nmock2 和事件期望

发布于 2024-08-25 04:19:21 字数 1373 浏览 14 评论 0原文

我正在为一个遵循 MVP 模式的小型应用程序编写测试。

从技术上讲,我知道我应该在代码之前编写测试,但我需要快速智能地构建一个演示应用程序,所以我现在回到测试,然后再进行真正的开发。

简而言之,我正在尝试测试演示者,但是由于Internal.ExpectationException,我什至无法运行空测试。

意外调用事件分配时会引发异常。

这是演示者类,

   public LBCPresenter(IView view, IModel model)
   {
        m_model = model;

        m_model.BatteryModifiedEvent += new EventHandler(m_model_BatteryModifiedEvent);
   }

模型接口

    public interface IModel
    {
         event EventHandler BatteryModifiedEvent;
    }

,这是测试类,我看不出我缺少什么,我告诉 NMock 期待该事件......

    [TestFixture]
public class MVP_PresenterTester
{
    private Mockery mocks;
    private IView _mockView;
    private IViewObserver _Presenter;
    private IModel _mockModel;

    [SetUp]
    public void SetUp()
    {
        mocks = new Mockery();

        _mockView = mocks.NewMock<IView>();
        _mockModel = mocks.NewMock<IModel>();
        _Presenter = new LBCPresenter(_mockView, _mockModel);

    }

    [Test]
    public void TestClosingFormWhenNotDirty()
    {
         Expect.Once.On(_mockModel).EventAdd("BatteryModifiedEvent", NMock2.Is.Anything);

       //makes no difference if following line is commented out or not
       //mocks.VerifyAllExpectationsHaveBeenMet();
    }
}

每次我运行测试时,我都会得到相同的期望异常。

有什么想法吗?

Im in the process of writing a test for a small application that follows the MVP pattern.

Technically, I know I should have written the test before the code, but I needed to knock up a demo app quick smart and so im now going back to the test before moving on to the real development.

In short I am attempting to test the presenter, however I cannot even get an empty test to run due to an Internal.ExpectationException.

The exception is raised on a unexpected invocation of an event assignation.

Here is the presenter class,

   public LBCPresenter(IView view, IModel model)
   {
        m_model = model;

        m_model.BatteryModifiedEvent += new EventHandler(m_model_BatteryModifiedEvent);
   }

Model Interface

    public interface IModel
    {
         event EventHandler BatteryModifiedEvent;
    }

And here is the test class, I can't see what im missing, ive told NMock to expect the event...

    [TestFixture]
public class MVP_PresenterTester
{
    private Mockery mocks;
    private IView _mockView;
    private IViewObserver _Presenter;
    private IModel _mockModel;

    [SetUp]
    public void SetUp()
    {
        mocks = new Mockery();

        _mockView = mocks.NewMock<IView>();
        _mockModel = mocks.NewMock<IModel>();
        _Presenter = new LBCPresenter(_mockView, _mockModel);

    }

    [Test]
    public void TestClosingFormWhenNotDirty()
    {
         Expect.Once.On(_mockModel).EventAdd("BatteryModifiedEvent", NMock2.Is.Anything);

       //makes no difference if following line is commented out or not
       //mocks.VerifyAllExpectationsHaveBeenMet();
    }
}

Every time I run the test I get the same expectation Exception.

Any ideas?

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

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

发布评论

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

评论(1

揪着可爱 2024-09-01 04:19:22

我认为这是一个计时问题 - 您在测试设置中调用演示者构造函数 - 这意味着事件添加是在您的测试设置 EventAdd 期望之前发生的。

如果将调用移动到 EventAdd 期望下方的演示者构造函数,它应该可以工作

I think its a timing issue - you are calling the presenter constructor in the test Setup - this means the event add is happening before your test sets up the EventAdd expectation.

If you move your call to the presenter constructor below the EventAdd expection it should work

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