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