如何使用断言来验证异常被抛出? [英] How do I use Assert to verify that an exception has been thrown?

查看:1068
本文介绍了如何使用断言来验证异常被抛出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何使用断言(或其他测试类?)来验证异常被抛出?

How do I use Assert (or other Test class?) to verify that an exception has been thrown?

推荐答案

有关Visual Studio团队测试看来你应用的ExpectedException属性来测试的方法。

For "Visual Studio Team Test" it appears you apply the ExpectedException attribute to the test's method.

从文档样品在这里:单元测试与演练Visual Studio团队测试

[TestMethod]
[ExpectedException(typeof(ArgumentException),
    "A userId of null was inappropriately allowed.")]
public void NullUserIdInConstructor()
{
   LogonInfo logonInfo = new LogonInfo(null, "P@ss0word");
}

这篇关于如何使用断言来验证异常被抛出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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