一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - ASP.NET教程 - mstest實現類似單元測試nunit中assert.throws功能

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

2019-11-27 13:53ASP.NET教程網 ASP.NET教程

我們做單元測試NUnit中,有一個斷言Assert.Throws很好用,現在我們來擴展一下也實現類似成功能,大家參考使用吧

我們做單元測試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 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)));
}
}


好了,現在我們在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");
 }
 

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 日韩欧美一卡二区 | 国产福利在线观看第二区 | 91综合精品网站久久 | 色综合久久综合网欧美综合网 | 天天色综合6 | 9999热视频| 娇妻被朋友征服中文字幕 | 摸进老太婆的裤裆小说 | 国产精品福利在线观看免费不卡 | 亚洲精品精品一区 | 国产成人精品免费久久久久 | 8天堂资源在线官网 | 高h肉爽文农民工 | 精品国产精品国产偷麻豆 | 色一情一区二区三区四区 | 女人张开腿让男人做爽爽 | 成人福利在线观看 | 激情三级做爰在线观看激情 | 国产午夜精品久久久久 | 幻女free性俄罗斯第一次摘花 | 国产真实伦对白在线播放 | 国产高清视频网站 | a级片在线播放 | 美女脱了内裤张开腿亲吻男生 | 日韩免费高清专区 | 香蕉久久久久 | 单亲乱l仑在线观看免费观看 | 色先锋影音先锋 | 精品高潮呻吟99AV无码 | 亚洲一区二区三区深夜天堂 | 精品一成人岛国片在线观看 | 色淫阁小说 | 四虎最新永久在线精品免费 | 日本网络视频www色高清免费 | 精品久久久久久亚洲精品 | 日本人成在线视频免费播放 | 午夜小视频网站 | 国产精品成人一区二区 | a毛片久久免费观看 | 欧美不卡一区二区三区 | 日韩一区二区在线视频 |