We do unit test NUnit, there's an assertion that assert.throws works, but when we use mstest you need to write this:
Copy Code code as follows:
[TestMethod]
[ExpectedException (typeof (ArgumentNullException))]
public void Writetotextfile ()
{
Pdfutility.writetotextfile ("d:\\aca.pdf", null);
}
Now let's expand it and implement a similar function, adding a class with the following code:
Copy Code code as follows:
<summary>
Useful assertions for the actions that is are expected to throw a exception.
</summary>
public static Class Exceptionassert
{
<summary>
Executes an exception, expecting a 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 a 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 ' 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 have here, the expected exception is not thrown. fail!
throw new Assertfailedexception (message??) "Expected exception is not thrown.");
}
<summary>
Executes an exception, expecting a exception of a specific type to is 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 a exception of a specific type to is thrown.
Like Assert.throws in NUnit.
</summary>
<param name= "Action" >the action to Execute</param>
<param name= ' message ' >the error message if ' 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 have here, the expected exception of type ' T ' is not thrown. fail!
throw new Assertfailedexception (message??) String.Format ("expected exception of type {0} not thrown.", typeof (T)));
}
}
OK, now we can do this in MSTest, look at the following code:
Copy Code code as follows:
[TestMethod]
public void WriteToTextFile2 ()
{
Implement Assert.throws in MSTest
Exceptionassert.throws<argumentnullexception> (() => pdfutility.writetotextfile ("D:\\ACA.pdf", NULL)
, "Output file path should not being null");
}