时间:2016-02-15 21:35 来源: 我爱IT技术网 作者:佚名
欢迎您访问我爱IT技术网,今天小编为你分享的编程技术是:【mstest实现类似单元测试nunit中assert.throws功能】,下面是详细的讲解!
mstest实现类似单元测试nunit中assert.throws功能
/// <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实现类似单元测试nunit中assert.throws功能的用户互动如下:
相关问题:
答: >>详细
相关问题:
答: >>详细
相关问题:
答: >>详细
- 【创建】ASP.NET Web API教程 创建域模型的方法详
- 【服务器】asp.net页面状态管理cookie和服务器状
- 【Repeater控件】.NET实现Repeater控件+AspNetPag
- 【客户端】获取客户端IP地址c#/vb.net各自实现代
- 【asp】asp.net上传execl文件后 在页面上加载显示
- 【Excel】页面导出为Excel的时间格式的问题-时间
- 【ref】asp.net(c#)ref out params的区别-out-pa
- 【数据控件】asp.net获得数据控件事件索引并获取
- 【NET】10个.NET中删除空白字符串的方法-删除空白
- 【web】web.config配置连接字符串的方法-config配
- 评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
-
