I'm unit testing a demo application, which is a POP3 client. The POP3 client implements IDisposable, so I'm trying to test a 'using' cycle.
(I'm using nunit 2.5.2 and Moq 4.0)
/// <summary>
/// Unsuccesfull construct dispose cycle, no ILogger object given.
/// Expecting ArgumentNullException. Expecting TcpClient dispose to be called.
/// </summary>
[Test]
[ExpectedException(typeof(ArgumentNullException))]
public void ConstructorDispose_LoggerNull_ArgumentNullException()
{
mockTcpClient.Setup(x => x.Dispose());
using (var popClient = new PopClient(null, mockTcpClient.Object))
{
}
mockTcpClient.VerifyAll();
}
As you can see the verifyAll method will never be invoked and the test will be succefull, non the less. What is the best way to solve this? Is there another way then try catch?
Update I fixed it like this for the moment:
mockTcpClient.Setup(x => x.Dispose());
var correctExceptionThrown = false;
try
{
using (var popClient = new PopClient(null, mockTcpClient.Object))
{
}
}
catch (ArgumentNullException)
{
correctExceptionThrown = true;
}
finally
{
mockTcpClient.VerifyAll();
}
Assert.That(correctExceptionThrown);
But dispose isn't called, seems to be the c# specification.