<PackageReference Include="NUnit" Version="4.3.1" />

ThrowsNothingConstraint

ThrowsNothingConstraint tests that a delegate does not throw an exception.
using NUnit.Framework.Internal; using System; using System.Runtime.CompilerServices; using System.Threading.Tasks; namespace NUnit.Framework.Constraints { [NullableContext(1)] [Nullable(0)] public class ThrowsNothingConstraint : Constraint { public override string Description => "No Exception to be thrown"; public override ConstraintResult ApplyTo<[Nullable(2)] TActual>(TActual actual) { Delegate parameterlessDelegate = ConstraintUtils.RequireActual<Delegate>(actual, "actual", false); Exception ex = ExceptionHelper.RecordException(parameterlessDelegate, "actual"); return new ConstraintResult(this, ex, ex == null); } public override ConstraintResult ApplyTo<[Nullable(2)] TActual>(ActualValueDelegate<TActual> del) { return ApplyTo((Delegate)del); } [AsyncStateMachine(typeof(<ApplyToAsync>d__4<>))] public override Task<ConstraintResult> ApplyToAsync<[Nullable(2)] TActual>(Func<Task<TActual>> taskDel) { <ApplyToAsync>d__4<TActual> stateMachine = default(<ApplyToAsync>d__4<TActual>); stateMachine.<>t__builder = AsyncTaskMethodBuilder<ConstraintResult>.Create(); stateMachine.<>4__this = this; stateMachine.taskDel = taskDel; stateMachine.<>1__state = -1; stateMachine.<>t__builder.Start(ref stateMachine); return stateMachine.<>t__builder.Task; } public ThrowsNothingConstraint() : base(Array.Empty<object>()) { } } }