使 DynamicMock MockInstance 等于其自身

发布于 2024-09-29 18:59:35 字数 1539 浏览 3 评论 0 原文

尝试使用 NUnit 测试将对象添加到队列的方法,如果该对象已排队,则抛出异常,但它失败,因为 Queue.Contains() 无法检测到模拟对象已在队列中。

被测试的方法非常简单:

public void Enqueue(ISomeInterface obj) {
    if (myQueue.Contains(obj)) {
        throw new InvalidOperationException("Already queued");
    }
    myQueue.Enqueue(obj);
}

测试也是如此:

[Test()]
public void TestQueueAlreadyQueued()
{
    DynamicMock mock = new DynamicMock(typeof (ISomeInterface));
    ISomeInterface obj = (ISomeInterface) mock.MockInstance;
    queueulator.Enqueue(obj);
    try {
        queueulator.Enqueue(obj);
        Assert.Fail("Exception expected");
    } catch (InvalidOperationException e) {
        // test passed
    }
}

这失败了 -- myQueue.Contains(obj) 总是返回 false,即使其他测试证明它正在被添加到队列中。

如果我在测试中添加以下断言 --

    Assert.AreEqual(obj, obj);

-- 它会失败。

我尝试添加 mock.ExpectAndReturn("Equals", true, obj) 但这似乎不起作用 - 我得到“太多调用 Equals / Expected: True / But was : 错误的”。

坦率地说,我不在乎调用 Equals 多少次——我并不想编写那么严格的测试。有没有一种简单的方法可以设置 Equals 使其表现“正常”?

(顺便说一句,是否有我应该使用的更高级的 .NET 模拟库?我是 .NET 的新手,在 Java 中使用 Mockito 之类的东西后,NUnit.Mocks 看起来很漂亮 2005 年。


)预计到达时间:在看到Moq ://monkeyisland.pl/2008/02/01/deathwish/" rel="nofollow">来自 Mockito 作者的有利说明;代码不再那么混乱,并且 Contains() 可以工作,所以这是一个开始。 (奇怪的是,AreEqual() 仍然失败。)

Trying to use NUnit to test a method that adds an object to a queue, and throws an exception if the object's already been queued, but it fails because Queue.Contains() can't detect that the mock object's already in the queue.

The method under test is pretty simple:

public void Enqueue(ISomeInterface obj) {
    if (myQueue.Contains(obj)) {
        throw new InvalidOperationException("Already queued");
    }
    myQueue.Enqueue(obj);
}

And so's the test:

[Test()]
public void TestQueueAlreadyQueued()
{
    DynamicMock mock = new DynamicMock(typeof (ISomeInterface));
    ISomeInterface obj = (ISomeInterface) mock.MockInstance;
    queueulator.Enqueue(obj);
    try {
        queueulator.Enqueue(obj);
        Assert.Fail("Exception expected");
    } catch (InvalidOperationException e) {
        // test passed
    }
}

This fails -- myQueue.Contains(obj) always returns false, even though other tests prove that it is being added to the queue.

If I add to the test the following assertion --

    Assert.AreEqual(obj, obj);

-- it fails.

I've tried adding mock.ExpectAndReturn("Equals", true, obj) but that doesn't seem to do it -- I get "Too many calls to Equals / Expected: True / But was: False".

And frankly, I don't care how many times Equals is called -- I'm not trying to write a test that strict. Is there a simple way to set up Equals to behave "normally" here?

(And as a side note, is there a more advanced .NET mocking library that I should be using? I'm new to .NET, and after using things like Mockito in Java, NUnit.Mocks seems pretty 2005.)


ETA: I've started using Moq after seeing a favorable note from the author of Mockito; the code is a bit less cluttered and Contains() works, so that's a start. (Weirdly, AreEqual() still fails, though.)

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

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

发布评论

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

评论(2

痴梦一场 2024-10-06 18:59:35

我很好奇你在这里使用模拟的动机。如果您创建实现 ISomeInterface 的类的常规实例,您的测试似乎会更简单。我想在你的情况下一定不容易实例化具体类。如果您无法让它与模拟一起使用,解决方案是仅为此测试实现一个具体的类。

我没有使用过 nunit.mocks,我通常使用 Rhino Mocks,它通常工作得很好,而且 Moq 框架也很流行。

I'm curious as to your motivation for using a mock here. It seems like your test would be simpler if you created a regular instance of a class that implements ISomeInterface. I guess in your case there must be no easy to instantiate concrete classes. If you can't get it to work with mocks, a solution would be to implement a concrete class just for this test.

I've not used nunit.mocks, I normally use Rhino Mocks which generally works pretty well, and also the Moq framework is popular.

余生一个溪 2024-10-06 18:59:35

为了结束而回答自己——答案似乎是“使用起订量”。

Answering myself in order to close -- the answer seems to be "use Moq."

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