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

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

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

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

最新評論

巴彦县| 贞丰县| 上林县| 柳林县| 金寨县| 深圳市| 五华县| 扶沟县| 瓮安县| 嘉义县| 葵青区| 顺义区| 宽城| 二连浩特市| 科技| 县级市| 马鞍山市| 正定县| 和田市| 岳池县| 来凤县| 深泽县| 仁化县| 南汇区| 仪陇县| 长汀县| 寿阳县| 华坪县| 丰顺县| 台安县| 聂拉木县| 寿光市| 河曲县| 邹平县| 名山县| 抚顺县| 广宁县| 建昌县| 天门市| 高台县| 湖北省|