/// <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); }
/// <summary> /// Asserts that a validation for a null argument happened. /// </summary> /// /// <param name="actionAssertions">A reference to the method or property.</param> /// <param name="argumentName">The name of the argument to be verified.</param> public static ExceptionAssertions <ArgumentNullException> ThrowArgumentNullException(this ActionAssertions actionAssertions, string argumentName) => actionAssertions .ThrowExactly <ArgumentNullException>() .WithMessage($"*{argumentName}*");
public static ExceptionAssertions <ArgumentException> ThrowInvalidGraphQLNameException(this ActionAssertions actionShould, string argumentName, string because) { using (new AssertionScope()) { var exceptionAssertions = actionShould.ThrowExactly <ArgumentException>(because) .WithMessage("Provided name does not comply with GraphQL's name specification.*"); exceptionAssertions.And.ParamName.Should().Be(argumentName, "because this is the argument that violates GraphQL's name specification"); exceptionAssertions.And.HelpLink.Should().Be("https://spec.graphql.org/June2018/#Name", "because this is the GraphQL Name specification"); return(exceptionAssertions); } }