如何使用 Assert.Throws 来断言异常的类型?
如何使用 Assert.Throws
来断言异常的类型和实际的消息措辞?
像这样的事情:
Assert.Throws<Exception>(
()=>user.MakeUserActive()).WithMessage("Actual exception message")
我正在测试的方法抛出多个具有不同消息的相同类型的消息,并且我需要一种方法来测试是否根据上下文抛出正确的消息。
How do I use Assert.Throws
to assert the type of the exception and the actual message wording?
Something like this:
Assert.Throws<Exception>(
()=>user.MakeUserActive()).WithMessage("Actual exception message")
The method I am testing throws multiple messages of the same type, with different messages, and I need a way to test that the correct message is thrown depending on the context.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
Assert.Throws
返回引发的异常,让您可以对异常进行断言。因此,如果没有抛出异常,或者抛出错误类型的异常,则第一个
Assert.Throws
断言将失败。但是,如果抛出正确类型的异常,那么您现在可以对保存在变量中的实际异常进行断言。通过使用此模式,您可以对异常消息之外的其他内容进行断言,例如,在
ArgumentException
及其衍生物的情况下,您可以断言参数名称是正确的:您还可以使用 Fluent API 执行以下操作 :这些断言:
或者,
对异常消息进行断言时的一个小技巧是使用 SetCultureAttribute 来修饰测试方法,以确保抛出的消息使用预期的区域性。如果您将异常消息存储为资源以允许本地化,这一点就会发挥作用。
Assert.Throws
returns the exception that's thrown which lets you assert on the exception.So if no exception is thrown, or an exception of the wrong type is thrown, the first
Assert.Throws
assertion will fail. However if an exception of the correct type is thrown then you can now assert on the actual exception that you've saved in the variable.By using this pattern you can assert on other things than the exception message, e.g. in the case of
ArgumentException
and derivatives, you can assert that the parameter name is correct:You can also use the fluent API for doing these asserts:
or alternatively
A little tip when asserting on exception messages is to decorate the test method with the
SetCultureAttribute
to make sure that the thrown message is using the expected culture. This comes into play if you store your exception messages as resources to allow for localization.您现在可以使用
ExpectedException
属性,例如You can now use the
ExpectedException
attributes, e.g.实际有效的解决方案:
这适用于
using Microsoft.VisualStudio.TestTools.UnitTesting;
。A solution that actually works:
This works with
using Microsoft.VisualStudio.TestTools.UnitTesting;
.对于那些使用 NUnit 3.0 约束模型< /a> 并最终到达这里:
For those that are using the NUnit 3.0 Constraint Model and ended up here:
展开 persistence的答案,并提供更多NUnit的功能,你可以这样做:
示例:
To expand on persistent's answer, and to provide more of the functionality of NUnit, you can do this:
Examples:
由于我对一些新的 NUnit 模式的冗长感到不安,我使用类似的东西来创建对我个人来说更干净的代码:
用法是:
Since I'm disturbed by the verbosity of some of the new NUnit patterns, I use something like this to create code that is cleaner for me personally:
The usage is then:
我最近遇到了同样的事情,并建议 MSTest 使用此功能:
用法:
断言发生了特定异常(MSTest 中的 Assert.Throws)。
I recently ran into the same thing, and suggest this function for MSTest:
Usage:
There is more in Assert that a particular exception has occured (Assert.Throws in MSTest).
断言异常:
在 Junit 5 中:
在 Junit 4 中:
Asserting exception :
In Junit 5 :
In Junit 4: