我有一些情况下,我不在乎什么异常被抛出(只要有些异常被抛出)。不幸的是,
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忽略异常
发布评论