在xUnit.net忽略异常

编程入门 行业动态 更新时间:2024-10-24 01:51:55
本文介绍了在xUnit忽略异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一些情况下,我不在乎什么异常被抛出(只要有些异常被抛出)。不幸的是,

Assert.Throws<异常>(someDelegate);

不及格,除非异常的确切(所以不是派生类的实例)被抛出的一个实例。我知道我可以得到我想要的行为

异常异常= Record.Exception(someDelegate); Assert.NotNull(例外);

,但它不能读取正确的。我缺少的东西在的xUnit有我想要的行为?这里有两个测试,表明我的意思:

[事实] 公共无效Throws_exception_and_passes(){     异常异常= Record.Exception(         ()=> {抛出新的InvalidOperationException异常(); }     );     Assert.NotNull(例外); } [事实] 公共无效Throws_exception_and_fails(){     Assert.Throws<异常>(         ()=> {抛出新的InvalidOperationException异常(); }     ); }

解决方案

每这里的文档:

HTTP://xunit.$c$cplex/ Wiki页面标题= HowToUse和放大器; referringTitle =首页

您必须指定抛出你想要的异常类型。在一般情况下,这是很好的做法。你应该能够predict什么情景测试会抛出异常的类型。你应该能够设计出你们两个的方法和方式的测试,让你predict这一点。

有办法解决这个,像做了尝试发现自己,但你应该考虑改变你的设计了一下。

I have some cases where I don't care what exception is thrown (as long as some exception is thrown). Unfortunately,

Assert.Throws<Exception>(someDelegate);

doesn't pass unless exactly an instance of Exception (so not an instance of a derived class) is thrown. I know I can obtain the behavior I want with

Exception exception = Record.Exception(someDelegate); Assert.NotNull(exception);

but it doesn't read right. Am I missing something in xUnit that has the behavior I want? Here are two tests that indicate what I mean:

[Fact] public void Throws_exception_and_passes() { Exception exception = Record.Exception( () => { throw new InvalidOperationException(); } ); Assert.NotNull(exception); } [Fact] public void Throws_exception_and_fails() { Assert.Throws<Exception>( () => { throw new InvalidOperationException(); } ); }

解决方案

Per the documentation here:

xunit.codeplex/wikipage?title=HowToUse&referringTitle=Home

You have to specify the type of exception you want to be thrown. In general, this is good practice. You should be able to predict what scenarios a test would throw what type of exception. You should be able to design both you method and your test in a way that will allow you to predict this.

There are ways around this, like doing a try catch yourself, but you should look into changing your design a bit.

更多推荐

在xUnit.net忽略异常

本文发布于:2023-11-16 10:00:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1603270.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:异常   xUnit   net

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!