/// <summary> /// ThrowArgumentException /// </summary> public static void ThrowArgumentException(this ActionAssertions assert, string paramName) { var exception = assert.Throw <Exception>().And; switch (exception) { case ArgumentException aE: { aE.ParamName.Should().Be(paramName); break; } case AggregateException aggE: { aggE.InnerExceptions.Count.Should().Be(1); aggE.InnerException.Should().BeOfType <ArgumentException>(); var aE = (ArgumentException)aggE.InnerException; aE?.ParamName.Should().Be(paramName); break; } default: { throw new AssertionFailedException($"Expected type to be {typeof(ArgumentException)}, but found {exception.GetType()}."); } } }
/// <summary> /// Asserts that the <paramref name="actionAssertions"/> subject throws the exact exception (and not a derived exception type). /// </summary> /// <param name="actionAssertions">A reference to the method or property.</param> /// <typeparam name="TException"> /// The type of the exception it should throw. /// </typeparam> /// <param name="because"> /// A formatted phrase explaining why the assertion should be satisfied. If the phrase does not /// start with the word <i>because</i>, it is prepended to the message. /// </param> /// <param name="becauseArgs"> /// Zero or more values to use for filling in any <see cref="string.Format(string,object[])"/> compatible placeholders. /// </param> /// <returns> /// Returns an object that allows asserting additional members of the thrown exception. /// </returns> public static ExceptionAssertions <TException> ThrowExactly <TException>(this ActionAssertions actionAssertions, string because = "", params object[] becauseArgs) where TException : Exception { var exceptionAssertions = actionAssertions.Throw <TException>(because, becauseArgs); exceptionAssertions.Which.GetType().Should().Be <TException>(because, becauseArgs); return(exceptionAssertions); }