mstest实现类似单元测试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");
 }
 

时间: 2024-08-24 22:22:25

mstest实现类似单元测试nunit中assert.throws功能_实用技巧的相关文章

mstest实现类似单元测试nunit中assert.throws功能

 我们做单元测试NUnit中,有一个断言Assert.Throws很好用,现在我们来扩展一下也实现类似成功能,大家参考使用吧 我们做单元测试NUnit中,有一个断言Assert.Throws很好用,但当我们使用MsTest时你需要这样写:   代码如下: [TestMethod] [ExpectedException(typeof(ArgumentNullException))] public void WriteToTextFile() { PDFUtility.WriteToTextFile

asp.net使用FCK编辑器中的分页符实现长文章分页功能_实用技巧

本文实例讲述了asp.net使用FCK编辑器中的分页符实现长文章分页功能.分享给大家供大家参考,具体如下: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="SplitContent.aspx.cs" Inherits="SplitContent" %> <%@ Register Assembly="FredCK.FCKeditorV2&

ASP.NET中实现模板页_实用技巧

使用过JavaStruts的程序员应该知道,Struts中有一个模板标记,虽然其使用起来比较复杂,但对于有大量页面的程序来说其带来了很多方便.但是,在ASP.NET中并没有现成实现可用,这方面国内讨论的也很少,其实,重写sender方法我们也可以实现类似的功能.下面是我项目中使用的一个基类模板类.  1using System; 2using System.Web.UI; 3 4namespace RepeaterDemo.Web.PageTemplate 5{ 6 /**//// <summa

“/”应用程序中的服务器错误_实用技巧

"/"应用程序中的服务器错误.  --------------------------------------------------------------------------------  运行时错误  说明: 服务器上出现应用程序错误.此应用程序的当前自定义错误设置禁止远程查看应用程序错误的详细信息(出于安全原因).但可以通过在本地服务器计算机上运行的浏览器查看.  详细信息: 若要使他人能够在远程计算机上查看此特定错误信息的详细信息,请在位于当前 Web 应用程序根目录下的

关于C#中DateTime常用方法概述_实用技巧

DateTime.Now.ToShortTimeString() DateTime dt = DateTime.Now; dt.ToString();//2005-11-5 13:21:25 dt.ToFileTime().ToString(); //127756416859912816 dt.ToFileTimeUtc().ToString();//127756704859912816 dt.ToLocalTime().ToString();//2005-11-5 21:21:25 dt.To

asp.net Silverlight中的模式窗体_实用技巧

其实在Silverlight中开发模式窗体并不难,比在Html里面用div来构造容易多了,但是要做到具有重用性和规范性还是要下一点工夫的.如果SL的开发朋友们想偷一点懒,直接用些现成写好的模式窗体代码的话,我在这里介绍一个SL的框架,叫SilverlightFX,里面就有一个Form类,只要你的xaml类继承了Form类就可以很方便地使用模式窗体了.具体方面可以参照他的sample工程,这里给出SilverlightFX的连接给大家 http://projects.nikhilk.net/Sil

ASP.NET MVC中的AJAX应用_实用技巧

一.ASP.NET MVC中的AJAX应用 首先,在ASP.NET MVC中使用自带的ajax功能,必须要导入2个js文件(顺序不能颠倒): ASP.NET MVC提供了2个常用的ajax辅助方法. Ajax.ActionLink 该辅助方法用于在页面上生成具有ajax功能的超链接. 在该辅助方法中有一个AjaxOptions类型的参数,它包括如下属性: Confirm:在发送ajax请求前,弹出确认对话框,该属性就是设置对话框中的提示消息HttpMethod:用于设置请求的类型:Get|Pos

asp.net中如何实现水印_实用技巧

水印是为了防止别盗用我们的图片. 两种方式实现水印效果 1)可以在用户上传时添加水印. a) 好处:与2种方法相比,用户每次读取此图片时,服务器直接发送给客户就行了. b) 缺点:破坏了原始图片. 2)通过全局的一般处理程序,当用户请求这张图片时,加水印. a) 好处:原始图片没有被破坏 b) 缺点:用户每次请求时都需要对请求的图片进行加水印处理,浪费的服务器的资源. 代码实现第二种方式: 复制代码 代码如下: using System; using System.Collections.Gen

ASP.NET中Ajax怎么使用_实用技巧

在ASP.NET中应用Ajax的格式如下: 前台代码(用JQuery库) $.ajax({ type: "POST", async: true, url: "../Ajax/ajax.ashx", dataType: "html", data: null success: function (result) { //do successful sth }, error: function (XMLHttpRequest, textStaus, e