最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

mstest實(shí)現(xiàn)類似單元測試nunit中assert.throws功能

 更新時(shí)間:2014年01月22日 14:10:24   作者:  
我們做單元測試NUnit中,有一個(gè)斷言Assert.Throws很好用,現(xiàn)在我們來擴(kuò)展一下也實(shí)現(xiàn)類似成功能,大家參考使用吧

我們做單元測試NUnit中,有一個(gè)斷言Assert.Throws很好用,但當(dāng)我們使用MsTest時(shí)你需要這樣寫:

復(fù)制代碼 代碼如下:

[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void WriteToTextFile()
{
PDFUtility.WriteToTextFile("D:\\ACA.pdf", null);
}

現(xiàn)在讓我們來擴(kuò)展一下也實(shí)現(xiàn)類似成功能,增加一個(gè)類,代碼如下:

復(fù)制代碼 代碼如下:

/// <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中可以這樣了,看下面代碼:
復(fù)制代碼 代碼如下:

[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)文章

  • CorFlags.exe檢查.NET程序平臺目標(biāo)(Platform Target)的工具

    CorFlags.exe檢查.NET程序平臺目標(biāo)(Platform Target)的工具

    .NET Framework SDK中的一個(gè)工具程序: CorFlags.exe。CorFlags.exe不但可查詢.NET組件的平臺目標(biāo)設(shè)定,甚至能直接修改設(shè)定,省去重新編譯的工夫。
    2013-02-02
  • .Net Core使用Socket與樹莓派進(jìn)行通信詳解

    .Net Core使用Socket與樹莓派進(jìn)行通信詳解

    這篇文章主要為大家詳細(xì)介紹了.Net Core使用Socket與樹莓派進(jìn)行通信,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-09-09
  • asp.net 自動將漢字轉(zhuǎn)換成拼音第一個(gè)字母

    asp.net 自動將漢字轉(zhuǎn)換成拼音第一個(gè)字母

    把漢字轉(zhuǎn)換成拼音第一個(gè)字母 的實(shí)現(xiàn)代碼
    2009-03-03
  • .net讀寫xml文檔詳解

    .net讀寫xml文檔詳解

    這篇文章主要介紹了.net讀寫xml文檔的示例,需要的朋友可以參考下
    2014-05-05
  • 淺談AjaxPro.dll,asp.net 前臺js調(diào)用后臺方法

    淺談AjaxPro.dll,asp.net 前臺js調(diào)用后臺方法

    這篇文章主要介紹了淺談AjaxPro.dll,asp.net 前臺js調(diào)用后臺方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • 詳解ASP.NET Core 反向代理部署知多少

    詳解ASP.NET Core 反向代理部署知多少

    這篇文章主要介紹了詳解ASP.NET Core 反向代理部署知多少,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-10-10
  • asp.net解決上傳4M文件限制

    asp.net解決上傳4M文件限制

    只需要在服務(wù)器上修改如下文件就可以使asp.net突破上傳4M文件的限制,大家參考使用吧
    2014-01-01
  • 刪除特殊字符和限定用戶輸入長度的示例代碼

    刪除特殊字符和限定用戶輸入長度的示例代碼

    在填寫注冊表單時(shí)工程師妹都會考慮到刪除特殊字符和限定用戶輸入長度等等,下面有個(gè)不錯(cuò)的示例,感興趣的朋友可以參考下
    2013-10-10
  • ASP.NET MVC中使用log4net的實(shí)現(xiàn)示例

    ASP.NET MVC中使用log4net的實(shí)現(xiàn)示例

    這篇文章主要介紹了ASP.NET MVC中使用log4net的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-01-01
  • .NET連接數(shù)據(jù)庫以及基本的增刪改查操作教程

    .NET連接數(shù)據(jù)庫以及基本的增刪改查操作教程

    這篇文章主要給大家介紹了關(guān)于.NET連接數(shù)據(jù)庫以及基本的增刪改查操作教程的相關(guān)資料,對于剛?cè)腴T的新手們來說是個(gè)很好的入門教程,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2018-01-01

最新評論

关岭| 鄂温| 泰安市| 宜兰县| 吴忠市| 老河口市| 大余县| 西乌珠穆沁旗| 沙河市| 色达县| 永吉县| 皋兰县| 临颍县| 房山区| 安乡县| 都安| 莱阳市| 山东| 绥化市| 临沭县| 凌云县| 体育| 宁阳县| 蒙阴县| 璧山县| 江北区| 综艺| 游戏| 灵丘县| 宜春市| 吉安县| 嵊泗县| 慈利县| 武汉市| 花垣县| 蒙山县| 武宣县| 孟州市| 济阳县| 洪泽县| 文安县|