using System; using System.Threading.Tasks; using Xunit; namespace Util.Domain.Tests.XUnitHelpers; /// /// 断言辅助操作 /// public class AssertHelper { /// /// 抛出异常,并从异常消息中搜索特定关键字 /// /// 异常类型 /// 操作 /// 关键字 public static TException Throws( Action action,string keyword = "" ) where TException : Exception { var exception = Assert.Throws( action ); if( !string.IsNullOrWhiteSpace( keyword ) ) Assert.Contains( keyword, exception.Message ); return exception; } /// /// 抛出异常,并从异常消息中搜索特定关键字 /// /// 异常类型 /// 操作 /// 关键字 public static async Task ThrowsAsync( Func action, string keyword = "" ) where TException : Exception { var exception = await Assert.ThrowsAsync( action ); if( !string.IsNullOrWhiteSpace( keyword ) ) Assert.Contains( keyword, exception.Message ); return exception; } }