mstest實現(xiàn)類似單元測試nunit中assert.throws功能
我們做單元測試NUnit中,有一個斷言Assert.Throws很好用,但當我們使用MsTest時你需要這樣寫:
[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void WriteToTextFile()
{
PDFUtility.WriteToTextFile("D:\\ACA.pdf", null);
}
現(xiàn)在讓我們來擴展一下也實現(xiàn)類似成功能,增加一個類,代碼如下:
/// <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)));
}
}
好了,現(xiàn)在我們在MsTest中可以這樣了,看下面代碼:
[TestMethod]
public void WriteToTextFile2()
{
//Implement Assert.Throws in MSTest
ExceptionAssert.Throws<ArgumentNullException>(()=> PDFUtility.WriteToTextFile("D:\\ACA.pdf", null)
,"Output file path should not be null");
}
相關(guān)文章
asp.net 程序性能優(yōu)化的七個方面 (c#(或vb.net)程序改進)
在我們開發(fā)asp.net過程中,需要注意的一些細節(jié),以達到我們優(yōu)化程序執(zhí)行效率。2009-03-03ASP.NET Core應(yīng)用錯誤處理之ExceptionHandlerMiddleware中間件呈現(xiàn)“定制化錯誤頁面”
這篇文章主要給大家介紹了關(guān)于ASP.NET Core應(yīng)用錯誤處理之ExceptionHandlerMiddleware中間件呈現(xiàn)“定制化錯誤頁面”的相關(guān)資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考借鑒,下面來一起看看吧2019-01-01比較簡單的將數(shù)據(jù)信息導(dǎo)入wrod文檔方案(C# for word)
史上最簡單將數(shù)據(jù)信息導(dǎo)入wrod文檔方案(C# for word)2010-01-01Entity Framework Core延遲加載(懶加載)用法
這篇文章介紹了Entity Framework Core延遲加載(懶加載)的使用方式,文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-02-02asp.net繼承IHttpHandler接口實現(xiàn)給網(wǎng)站圖片添加水印功能實例
這篇文章主要介紹了asp.net繼承IHttpHandler接口實現(xiàn)給網(wǎng)站圖片添加水印功能,實例分析了asp.net基于IHttpHandler接口實現(xiàn)網(wǎng)站圖片水印功能的具體步驟與相關(guān)技巧,需要的朋友可以參考下2016-07-07狀態(tài)保存機制之ViewState概述及應(yīng)用
無狀態(tài)的根本原因是:瀏覽器和服務(wù)器使用Socket通信,服務(wù)器將請求結(jié)果返回給瀏覽器后,會關(guān)閉當前Socket連接,接下來介紹狀態(tài)保存機制,感興趣的朋友可以了解下2013-02-02ASP.NET Core項目結(jié)構(gòu)教程(4)
這篇文章主要為大家詳細介紹了ASP.NET Core項目結(jié)構(gòu),具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-06-06