好湿?好紧?好多水好爽自慰,久久久噜久噜久久综合,成人做爰A片免费看黄冈,机机对机机30分钟无遮挡

主頁 > 知識庫 > mstest實現類似單元測試nunit中assert.throws功能

mstest實現類似單元測試nunit中assert.throws功能

熱門標簽:400電話辦理信任翰諾科技 怎樣給陜西地圖標注顏色 ai電銷機器人對貸款有幫助嗎 云狐人工智能電話機器人 廣州銷售外呼系統定制 宿遷智能外呼系統排名 電銷機器人 數據 福州人工智能電銷機器人加盟 地圖標注多少錢一張

我們做單元測試NUnit中,有一個斷言Assert.Throws很好用,但當我們使用MsTest時你需要這樣寫:

復制代碼 代碼如下:

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

現在讓我們來擴展一下也實現類似成功能,增加一個類,代碼如下:

復制代碼 代碼如下:

/// 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 ThrowsT>(Action action) where T : Exception
{
return ThrowsT>(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 ThrowsT>(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)));
}
}

好了,現在我們在MsTest中可以這樣了,看下面代碼:
復制代碼 代碼如下:

[TestMethod]
 public void WriteToTextFile2()
{
//Implement Assert.Throws in MSTest
ExceptionAssert.ThrowsArgumentNullException>(()=> PDFUtility.WriteToTextFile("D:\\ACA.pdf", null)
 ,"Output file path should not be null");
 }
 

標簽:焦作 大興安嶺 綿陽 宜春 延安 新疆 黃南 曲靖

巨人網絡通訊聲明:本文標題《mstest實現類似單元測試nunit中assert.throws功能》,本文關鍵詞  mstest,實現,類似,單元,測試,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《mstest實現類似單元測試nunit中assert.throws功能》相關的同類信息!
  • 本頁收集關于mstest實現類似單元測試nunit中assert.throws功能的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 老太爷破小丫鬟苞h文| 久久综合亚洲鲁鲁五月天欧美| 亚洲精品久久久久久精品桃花岛图片 | 成人精品鲁一鲁一区二区| 香蕉漫画网站在线入口| 我要再来一次| 岳在我胯下哭泣| 欧洲影坛第一美女的5部大尺度电影 | 少妇高潮喷水久久久久久久久| 4410看吧影院| 精品国产乱码久久久软件使用方法 | 国产a片| 在线观看吃瓜AV网站| 亚洲 欧美 叧类人妖| 情糜小说| 婬荡的秘书呻吟波多野结衣| 91在线无码精品秘?国产三年| 欧美精品小视频| 疯狂三p群体交乱视频| 91理论片午午伦夜理片久| 欧美日韩激情在线| 欧美性动图| 儿子的女朋友4| 欧美在线视频不卡| 欧美群妇大交乱婬| 亚洲人成激情在线播放| 六月激情网| 美国式禁忌1—4观看中文| 办公室性过程很黄的小说| 动漫迷秘?黄www免费观看| 亲胸揉胸膜下刺激视频高| 坏蛋是怎样炼成的1全文免费阅读| 老师沦为学生胯下奴h| 久久国产免费观看精品3| 欧美夫妻性生活片| 啊 啊 用力 别停老师 小说| 免费看男男GAY啪啪网站| 男男gaytwinkfreevideos动漫| 久久狠狠躁免费观看| 我被闺蜜捆绑穿女仆装调教的小说| 久久99国产精品久久99 作者|