Rhino 在 NUnit 中模拟调用而不是录音

发布于 2024-07-26 22:29:05 字数 1285 浏览 1 评论 0原文

我正在尝试为一些涉及事件的代码编写单元测试。 由于我需要随意引发一个事件,我决定依靠 RhinoMocks 来为我做到这一点,然后确保引发的事件的结果符合预期(当他们单击按钮时,值应该在以可预测的方式,在本例中,对象的高度应该降低)

因此,我做了一些研究并意识到我需要一个针对相关事件的事件引发器。 那么就像调用 eventraiser.Raise(); 一样简单 我们很好。

我编写的用于获取事件引发程序的代码如下(用 C# 编写)(或多或少直接从网上复制),

            using (mocks.Record())
        {
            MyControl testing = mocks.DynamicMock<MyControl>();
            testing.Controls.Find("MainLabel",false)[0].Click += null;
            LastCall.IgnoreArguments();
            LastCall.Constraints(Rhino.Mocks.Constraints.Is.NotNull());
            Raiser1 = LastCall.GetEventRaiser();
        }

然后我将其测试为“在播放模式下”。

            using (mocks.Playback())
        {
            MyControl thingy = new MyControl();
            int temp=thingy.Size.Height;
            Raiser1.Raise();
            Assert.Greater(temp, thingy.Size.Height);
        }

问题是,当我通过 NUnit 运行这些测试时,它失败了。 它在 test.Controls.Find("MainLabel",false)[0].Click += null; 行抛出异常。 它抱怨尝试将 null 添加到事件侦听器。 具体来说,“System.NullReferenceException:对象引用未设置为对象的实例”

现在,我了解到 Mocks.Record 标题下的任何代码实际上都不会被调用,而是会创建对以下代码调用的期望播放。 然而,这是我遇到这样的问题的第二个实例(第一个问题涉及更复杂的类/案例),在 NUnit 中,代码实际上被正常调用,而不是创建期望。 我很好奇是否有人可以指出我做错了什么。 或者用另一种方式来解决核心问题。

I am trying to write unit tests for a bit of code involving Events. Since I need to raise an event at will, I've decided to rely upon RhinoMocks to do so for me, and then make sure that the results of the events being raised are as expected (when they click a button, values should change in a predictable manner, in this example, the height of the object should decrease)

So, I do a bit of research and realize I need an Event Raiser for the event in question. Then it's as simple as calling eventraiser.Raise(); and we're good.

The code for obtaining an event raiser I've written as is follows (written in C#) (more or less copied straight off the net)

            using (mocks.Record())
        {
            MyControl testing = mocks.DynamicMock<MyControl>();
            testing.Controls.Find("MainLabel",false)[0].Click += null;
            LastCall.IgnoreArguments();
            LastCall.Constraints(Rhino.Mocks.Constraints.Is.NotNull());
            Raiser1 = LastCall.GetEventRaiser();
        }

I then test it as In playback mode.

            using (mocks.Playback())
        {
            MyControl thingy = new MyControl();
            int temp=thingy.Size.Height;
            Raiser1.Raise();
            Assert.Greater(temp, thingy.Size.Height);
        }

The problem is, when I run these tests through NUnit, it fails. It throws an exception at the line testing.Controls.Find("MainLabel",false)[0].Click += null; which complains about trying to add null to the event listener. Specifically, "System.NullReferenceException: Object Reference not set to an instance of the Object"

Now, I was under the understanding that any code under the Mocks.Record heading wouldn't actually be called, it would instead create expectations for code calls in the playback. However, this is the second instance where I've had a problem like this (the first problem involved classes/cases that where a lot more complicated) Where it appears in NUnit that the code is actually being called normally instead of creating expectations. I am curious if anyone can point out what I am doing wrong. Or an alternative way to solve the core issue.

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

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

发布评论

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

评论(1

一袭水袖舞倾城 2024-08-02 22:29:05

我不确定,但如果您没有在 MyControl 中将事件设为虚拟,您可能会遇到这种行为。 如果方法、事件或属性不是虚拟的,那么我认为 DynamicMock 无法用录制和播放版本替换它们的行为。

就我个人而言,我喜欢为我要模拟的类定义接口,然后模拟该接口。 这样,我就一定能够避免此类问题的发生。

I'm not sure, but you might get that behaviour if you haven't made the event virtual in MyControl. If methods, events, or properties aren't virtual, then I don't think DynamicMock can replace their behaviour with recording and playback versions.

Personally, I like to define interfaces for the classes I'm going to mock out and then mock the interface. That way, I'm sure to avoid this kind of problem.

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