/// <summary> /// Allows testing a specific property on the thrown exception /// </summary> /// <param name="continuation">Exception-based continuation</param> /// <param name="fetcher">Func to invoke to get the property you'd like to test</param> /// <typeparam name="T">Type of the Exception</typeparam> /// <typeparam name="TValue">Type of the property you're testing</typeparam> /// <returns>Throw continuation, used to continue with testing the property you just selected</returns> /// <exception cref="ArgumentException">Thrown if the continuation is not a known ThrowContinuation. Userland implementation of IThrowContinuation is not supported - yet. Make a request if you need it!</exception> public static IExceptionPropertyContinuation <TValue> With <T, TValue>( this IThrowContinuation <T> continuation, Func <T, TValue> fetcher ) where T : Exception { var throwContinuation = continuation as ThrowContinuation <T>; var actual = throwContinuation?.Exception ?? continuation.TryGetPropertyValue <T>("Exception"); if (actual == null) { // TODO: can we kinda-duck this to work on user-generated implementations of IThrowContinuation<T>? And do we care to? throw new ArgumentException( "With should operate on a ThrowContinuation<T> or at least something with an Exception property that is an Exception."); } var exceptionPropertyValue = fetcher(actual); return(ContinuationFactory.Create <TValue, ExceptionPropertyContinuation <TValue> >( exceptionPropertyValue, new WrappingContinuation <T, TValue>( throwContinuation, c => exceptionPropertyValue ) )); }
/// <summary> /// Verifies that the ArgumentException or derivative was thrown with the /// expected ParameterName. Effectively shorthand for /// Expect(func).To.Throw<ArgumentException>.With.Property(e => e.ParamName == parameterName); /// </summary> /// <param name="continuation">Continuation to operate on</param> /// <param name="parameterName">Expected parameter name</param> /// <typeparam name="T"></typeparam> /// <returns>Continuation which can be used to do more verification of the exception</returns> public static IThrowContinuation <T> For <T>( this IThrowContinuation <T> continuation, string parameterName ) where T : ArgumentException { continuation.AddMatcher(actual => { var passed = actual.ParamName == parameterName; return(new MatcherResult( passed, () => continuation.IsNegated() // not actually sure how we'd get here if negated & throwing ? $"Expected ArgumentException with ParameterName other than '{parameterName}', but found exactly that" : $"Expected ArgumentException with ParameterName '{parameterName}' but found '{actual.ParamName}'" )); }); return(continuation); }