Mon Jan 16 2006
While you cannot validate you exception's message in the ExpectedException attribute all is not lost.  Lets walk through three scenarios. In all three tests you will see an Assert.Fail in the try block.  You need this statement in case the target doesn't throw any exceptions, therefor failing the test.  The first example is a passing test, throw and exception with a message, catch it and compare the message string.     [TestMethod]    public void PassingException()    {       const string expectedExceptionMessage = "Foobar Message";       try       {          throw new ApplicationException( expectedExceptionMessage );          Assert.Fail( "Expected an exception to get thrown by target" );       }       catch ( ApplicationException ex )       {          Assert.AreEqual<string>( expectedExceptionMessage, ex.Message );       }    }   Here we see the same test failing only because the message contained in the exception is different.    [TestMethod]    public void FailingOnExceptionMessage()    {       const string expectedExceptionMessage = "Foobar Message";       try       {          throw new ApplicationException( "Invalid" );          Assert.Fail( "Expected an exception to get thrown by target" );       }       catch ( ApplicationException ex )       {          Assert.AreEqual<string>( expectedExceptionMessage, ex.Message );       }    } While this last test follows the same pattern seen above the test is really only testing for the type of exception being throw.  This should be refactored to use the ExpectedException attribute.    [TestMethod]    public void FailingOnExceptionType()    {       const string expectedExceptionMessage = "Foobar Message";       try       {          throw new Exception( expectedExceptionMessage );          Assert.Fail( "Expected an exception to get thrown by target" );       }       catch ( ApplicationException ex )       {            }       catch       {          Assert.Fail( "Incorrect Exception type was thrown by target" );       }    } Same test refactored, this test still fails but only to illustrate that example.    [TestMethod, ExpectedException ( typeof ( ApplicationException) )]    public void FailingOnExceptionType()    {       throw new Exception( "..." );    }