犀牛嘲笑命名期望

发布于 2024-08-15 10:34:55 字数 784 浏览 3 评论 0原文

我的测试对象有两个相同类型的依赖对象。有时,当测试的期望失败时,并不清楚哪个依赖对象设置了该期望。有没有某种方法可以给出将出现在错误消息中的依赖对象名称,以便我可以区分它们?

这是一个示例:

        MockRepository mocks = new MockRepository();
        var xAxis = mocks.StrictMock<IAxis>();
        var yAxis = mocks.StrictMock<IAxis>();
        Ball ball;

        using (mocks.Record())
        {
            Expect.Call(xAxis.Velocity).Return(100);
            Expect.Call(yAxis.Velocity).Return(0);
        }
        using (mocks.Playback())
        {
            ball = new Ball(xAxis, yAxis);
            ball.Bounce();
        }

现在,如果 Bounce 代码有问题,我可能会收到如下消息:

Rhino.Mocks.Exceptions.ExpectationViolationException: IAxis.get_Velocity();预期 #1,实际 #0。

我无法轻易判断哪个轴被错过了。

My object under test has two dependency objects of the same type. Sometimes when a test has a failed expectation, it's not clear which dependency object set that expectation. Is there some way to give the dependency objects names that will appear in the error messages so that I can tell them apart?

Here's an example:

        MockRepository mocks = new MockRepository();
        var xAxis = mocks.StrictMock<IAxis>();
        var yAxis = mocks.StrictMock<IAxis>();
        Ball ball;

        using (mocks.Record())
        {
            Expect.Call(xAxis.Velocity).Return(100);
            Expect.Call(yAxis.Velocity).Return(0);
        }
        using (mocks.Playback())
        {
            ball = new Ball(xAxis, yAxis);
            ball.Bounce();
        }

Now if there's something wrong with the Bounce code, I might get a message like this:

Rhino.Mocks.Exceptions.ExpectationViolationException :
IAxis.get_Velocity(); Expected #1, Actual #0.

I can't easily tell which axis got missed.

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

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

发布评论

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

评论(1

木格 2024-08-22 10:34:55

我找到了解决方案,但这并不完全是我所希望的。您可以为每个期望添加一条消息。我的例子变成:

        Expect.Call(xAxis.Velocity).Return(100).Message("x axis");
        Expect.Call(yAxis.Velocity).Return(0).Message("y axis");

现在异常更具描述性:

Rhino.Mocks.Exceptions.ExpectationViolationException:消息:x 轴
IAxis.get_Velocity();预期 #1,实际 #0。

唯一的缺点是我必须为每个期望添加一条消息。我希望只命名模拟对象,以便该名称出现在所有消息中。

I found a solution, but it's not quite what I had hoped for. You can add a message to each expectation. My example becomes:

        Expect.Call(xAxis.Velocity).Return(100).Message("x axis");
        Expect.Call(yAxis.Velocity).Return(0).Message("y axis");

And the exception is now more descriptive:

Rhino.Mocks.Exceptions.ExpectationViolationException : Message: x axis
IAxis.get_Velocity(); Expected #1, Actual #0.

The only down side is that I have to add a message for every expectation. I was hoping to just name the mock object so that that name would appear in all messages.

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