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

ThrowsExceptionConstraint

ThrowsExceptionConstraint tests that an exception has been thrown, without any further tests.
using NUnit.Framework.Internal; using System; using System.Runtime.CompilerServices; namespace NUnit.Framework.Constraints { [NullableContext(1)] [Nullable(0)] public class ThrowsExceptionConstraint : Constraint { [Nullable(0)] private class ThrowsExceptionConstraintResult : ConstraintResult { public ThrowsExceptionConstraintResult(ThrowsExceptionConstraint constraint, [Nullable(2)] Exception caughtException) : base(constraint, caughtException, caughtException != null) { } public override void WriteActualValueTo(MessageWriter writer) { if (base.Status == ConstraintStatus.Failure) writer.Write("no exception thrown"); else base.WriteActualValueTo(writer); } } public override string Description => "an exception to be thrown"; public override ConstraintResult ApplyTo<[Nullable(2)] TActual>(TActual actual) { Delegate parameterlessDelegate = ConstraintUtils.RequireActual<Delegate>(actual, "actual", false); Exception caughtException = ExceptionHelper.RecordException(parameterlessDelegate, "actual"); return new ThrowsExceptionConstraintResult(this, caughtException); } public override ConstraintResult ApplyTo<[Nullable(2)] TActual>(ActualValueDelegate<TActual> del) { return ApplyTo((Delegate)del); } public ThrowsExceptionConstraint() : base(Array.Empty<object>()) { } } }