如何使用 Assert 验证是否已抛出异常? [英] How do I use Assert to verify that an exception has been thrown?

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

问题描述

如何使用 Assert(或其他 Test 类)来验证是否已抛出异常?

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

推荐答案

对于Visual Studio Team Test",您似乎将 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");
}

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

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