We do unit test NUnit, there is an assertion assert.throws very good, now we expand the implementation of similar functions, we refer to the use of the bar
We do unit test NUnit, there is an assertion assert.throws very good, but when we use mstest you need to write: code as follows: [TestMethod] [ExpectedException ( ArgumentNullException)]] public void Writetotextfile () {pdfutility.writetotextfile ("d:aca.pdf", null);} Now let's extend this to also implement a similar functionality, add a class, the code is as follows: code as follows:///<summary>///useful assertions for actions that are expected to throw an exception. </summary> public static class Exceptionassert {///<summary>///executes a exception, expecting an exc Eption to be thrown. Like Assert.throws in NUnit. </summary>///<param name= "action" >the action to execute</param>///of <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 the expected exception ' not ' thrown</param>/// ; The exception thrown by the action</returns> public static exception Throws (Action action, string message) {try {a Ction (); The catch (Exception ex) {//The action method has thrown the expected Exception. Test wants to perform further assertions on it. return ex; } //If We are here, the expected exception is not thrown. fail! throw new Assertfailedexception (message??) "Expected exception is not thrown."); ///<summary>///executes a 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>///of <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 a 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>///the <param "message" > The error message if the expected exception isn't thrown</param>///<returns>the exception by the Act ion</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 are 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: 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 is null "); }