Xunit 删除了Assert.DoesNotThrow 断言方法,这在这种情况下是合适的。
您可以结合使用Record.Exception 和Assert.False 方法。
Assert.False,因为Assert.IsNotType<T> 方法没有自定义断言消息的重载
var exception = Record.ExceptionAsync(() => Blah());
Assert.False(exception is CertainTypeException, "Shouldn't throw, can fix it with ...");
使用 FluentAssertion 库,您可以按照以下方式进行操作
Func<Task> callBlah = () => Blah();
await callBlah.Should().NotThrowAsync("Shouldn't throw, can fix it with ...");
替代选项,在您的情况下,我更喜欢以前的选项,将潜在修复信息添加到异常消息中。
public class MyCertainException : Exception
{
public MyCertainException (string message) : base($"{message}. Can be fixed with...")
{
}
}
使用最后一种方法你什么都不用做,如果抛出异常,Xunit 将在输出结果中显示它的消息,其他开发人员在生产或调试过程中看到此类异常时也会看到潜在的修复。