title: Expecting an exception in a test but wanting to verify dispose is called anyway
body: <p>I'm unit testing a demo application, which is a POP3 client. The POP3 client implements <code>IDisposable</code>, so I'm trying to test a <code>using</code> cycle. </p>
  
  <p>(I'm using nunit 2.5.2 and Moq 4.0)</p>
  
  <pre><code>/// &lt;summary&gt;
  /// Unsuccessful construct dispose cycle, no ILogger object given. 
  /// Expecting ArgumentNullException. Expecting TcpClient dispose to be called.
  /// &lt;/summary&gt;
  [Test]
  [ExpectedException(typeof(ArgumentNullException))]
  public void ConstructorDispose_LoggerNull_ArgumentNullException()
  {
      mockTcpClient.Setup(x =&gt; x.Dispose());
      using (var popClient = new PopClient(null, mockTcpClient.Object))
      {
  
      }
      mockTcpClient.VerifyAll();
  }
  </code></pre>
  
  <p>As you can see the <code>verifyAll</code> method will never be invoked and the test will be successful, nonetheless. So ...</p>
  
  <ul>
  <li>What is the best way to solve this?</li>
  <li>Is there another way then try catch?</li>
  </ul>
  
  <p>Update I fixed it like this for the moment:</p>
  
  <pre><code>        mockTcpClient.Setup(x =&gt; x.Dispose());
          var correctExceptionThrown = false;
          try
          {
              using (var popClient = new PopClient(null, mockTcpClient.Object))
              {
  
              }
          }
          catch (ArgumentNullException)
          {
              correctExceptionThrown = true;
          }
          finally
          {
              mockTcpClient.VerifyAll();
          }
          Assert.That(correctExceptionThrown);
  </code></pre>
  
  <p><strong>But dispose isn't called</strong>, seems to be the C# specification.</p>