C#、NUnit:测试 ArgumentException 是否具有正确 ParamName 的清晰方法
要测试某些东西是否会抛出ArgumentException
,我可以这样做:
Assert.Throws<ArgumentException>(() => dog.BarkAt(deafDog));
How can I check that the ParamName
is Correct in a clear way?还有额外的问题:或者您可能会建议根本不测试这个?
To test that something throws for example an ArgumentException
I can do this:
Assert.Throws<ArgumentException>(() => dog.BarkAt(deafDog));
How can I check that the ParamName
is correct in a clear way? And bonus question: Or would you perhaps perhaps recommend not testing this at all?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
找到了一种非常清晰的方法(但如果有人有更好的方法,请告诉我!)
Facepalm< /a>...
Found a pretty clear way (but please let me know if anyone have an even better one!)
Facepalm...
如果您想对异常执行更多操作,而不仅仅是断言抛出该异常,则
Assert.Throws
实际上会返回该异常,您可以执行以下操作:If you want to do more with the exception than just assert that it is thrown, then
Assert.Throws
actually returns the exception and you can do this:跳转到12年后的未来->你现在可以这样做(使用 NUnit 3 的流利语法):
Skipping 12 years into the future -> you can now do it like this (using NUnit 3's fluent syntax):