简体   繁体   English

xUnit中的Legacy代码中的ExpectedException

[英]ExpectedException in xUnit for Legacy Code

As widely stated in other posts, ExpectedException does not exist in xUnit. 正如其他文章中广泛指出的那样,xUnit中不存在ExpectedException。 However, is there something that exists to assist in porting legacy tests to xUnit as essentially a polyfill? 但是,是否存在某种东西可以帮助将传统测试作为实质上的polyfill移植到xUnit?

I believe that with an accelerated adoption of .Net Core as we will likely see with .Net Standard 2, we will see more and more tests getting ported from other frameworks to those supported most easily out of the box in .Net Core tooling. 我相信,随着对.Net Core 2的加速采用(如在.Net Standard 2中所看到的那样),我们将看到越来越多的测试从其他框架移植到.Net Core工具中最容易获得的支持。 While ExpectedException is probably not best practice going forward, having a legacy stopgap (even as eg a separate NuGet package) would greatly ease porting tests as this is one of the few transformations that is on a per-test basis and cannot be done by a simple search-replace. 虽然ExpectedException可能不是最佳实践,但是具有遗留的权宜之计(即使是单独的NuGet程序包)也将大大简化移植测试的过程,因为这是每次测试中为数不多的转换之一,无法通过测试完成简单的搜索替换。 And when you're porting hundreds of unit tests, the "this can cause problems" doesn't seem like such a satisfying answer. 而且,当您要移植数百个单元测试时,“这可能会引起问题”似乎不是一个令人满意的答案。

I may have misunderstood your question, but you can test for exceptions in xUnit by doing something like this: 我可能误解了您的问题,但是您可以通过执行以下操作来测试xUnit中的异常:

[Fact]
public void ExceptionTest()
{
    // Arrange
    // Act
    Action act = () => throw new Exception();

    // Assert
    Assert.Throws<Exception>(act);
}

Allowing each ExpectedException test to be converted by adding in the xUnit Assert.Throws . 通过添加xUnit Assert.Throws来允许转换每个ExpectedException测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM