문제

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.)

도움이 되었습니까?

해결책 2

Answering myself in order to close -- the answer seems to be "use 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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top