In the unit test NUnit, we have an asserted Assert. Throws which is very useful, but when we use MsTest, you need to write it like this:
Copy codeThe Code is as follows:
[TestMethod]
[ExpectedException (typeof (ArgumentNullException)]
Public void WriteToTextFile ()
{
PDFUtility. WriteToTextFile ("D :\\ ACA.pdf", null );
}
Now let's extend the implementation to a similar success, and add a class. The Code is as follows:
Copy codeThe Code is as follows:
/// <Summary>
/// Useful assertions for actions that are expected to throw an exception.
/// </Summary>
Public static class ExceptionAssert
{
/// <Summary>
/// Executes an exception, expecting an exception to be thrown.
/// Like Assert. Throws in NUnit.
/// </Summary>
/// <Param name = "action"> The action to execute </param>
/// <Returns> The exception thrown by the action </returns>
Public static Exception Throws (Action action)
{
Return Throws (action, null );
}
/// <Summary>
/// Executes an exception, expecting an exception to be thrown.
/// Like Assert. Throws in NUnit.
/// </Summary>
/// <Param name = "action"> The action to execute </param>
/// <Param name = "message"> The error message if the expected exception is not thrown </param>
/// <Returns> The exception thrown by the action </returns>
Public static Exception Throws (Action action, string message)
{
Try
{
Action ();
}
Catch (Exception ex)
{
// The action method has thrown the expected exception.
// Return the exception, in case the unit test wants to perform further assertions on it.
Return ex;
}
// If we end up here, the expected exception was not thrown. Fail!
Throw new AssertFailedException (message ?? "Expected exception was not thrown .");
}
/// <Summary>
/// Executes an exception, expecting an exception of a specific type to be thrown.
/// Like Assert. Throws in NUnit.
/// </Summary>
/// <Param name = "action"> The action to execute </param>
/// <Returns> The exception thrown by the action </returns>
Public static T Throws <T> (Action action) where T: Exception
{
Return Throws <T> (action, null );
}
/// <Summary>
/// Executes an exception, expecting an exception of a specific type to be thrown.
/// Like Assert. Throws in NUnit.
/// </Summary>
/// <Param name = "action"> The action to execute </param>
/// <Param name = "message"> The error message if the expected exception is not thrown </param>
/// <Returns> The exception thrown by the action </returns>
Public static T Throws <T> (Action action, string message) where T: Exception
{
Try
{
Action ();
}
Catch (Exception ex)
{
T actual = ex as T;
If (actual = null)
{
Throw new AssertFailedException (message ?? String. Format ("Expected exception of type {0} not thrown. Actual exception type was {1}.", typeof (T), ex. GetType ()));
}
// The action method has thrown the expected exception of type 'T '.
// Return the exception, in case the unit test wants to perform further assertions on it.
Return actual;
}
// If we end up here, the expected exception of type 't'was not thrown. Fail!
Throw new AssertFailedException (message ?? String. Format ("Expected exception of type {0} not thrown.", typeof (T )));
}
}
Now we can use the following code in MsTest:
Copy codeThe Code is as follows:
[TestMethod]
Public void WriteToTextFile2 ()
{
// Implement Assert. Throws in MSTest
ExceptionAssert. Throws <ArgumentNullException> () => PDFUtility. WriteToTextFile ("D: \ ACA.pdf", null)
, "Output file path shocould not be null ");
}