测试自定义异常 [英] Testing Custom Exception

查看:121
本文介绍了测试自定义异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

异常框架设计指南需要一个例外:


SomeException(SerializationInfo info,StreamingContext context);

如何测试此异常?

顺便提一下对于任何想要单元测试框架设计指南要求的其他三种类型的例子的例子,请从 http:/获取CommonData项目/www.CodePlex.Com/CommonData


解决方案

您可以使用 ExpectedException 属性。通常,如果某个方法在传递某些参数时要抛出异常,我们会将具有此属性的例外异常设置为test方法。测试框架将检查是否抛出了正确的异常,因为我们除外。


The Framework Design Guidelines for Exceptions require an exception like:

protected SomeException(SerializationInfo info, StreamingContext context);

How should this exception be tested?

Incidentally for any one wanting an example of unit testing the other three types of exception as required by the Framework Design Guidelines, please get the CommonData project from http://www.CodePlex.Com/CommonData


解决方案

You can use the ExpectedException attribute. Generally, if a method is intended to throw an exception when we pass in certain parameters, we set the excepted exception with this attribute to the test method. The test framework will check if the correct exception is thrown as we excepted.


这篇关于测试自定义异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆