/// <summary> /// Asserts the specified test method throws an exception of the specified type. /// </summary> /// <typeparam name="TException">The <see cref="Type"/> of <see cref="ArgumentException"/> that should be thrown.</typeparam> /// <param name="assert">The extended <see cref="Asserter"/>.</param> /// <param name="testMethod">The <see cref="Action"/> representing the test method.</param> /// <param name="message">A message to display if the assertion fails. This message can be seen in the unit test results.</param> /// <param name="parameters">An array of parameters to use when formatting <paramref name="message" />.</param> /// <returns>The <see cref="ExceptionAsserter{T}">asserted exception</see>.</returns> /// <include file="examples.xml" path="Types/Type[@name='ExceptionExtensions']/Member[@name='Throws`1']/example[2]" /> public static ExceptionAsserter <TException> Throws <TException>(this Asserter assert, Action testMethod, string message, params object[] parameters) where TException : Exception { Arg.NotNull(assert, nameof(assert)); Contract.Ensures(Contract.Result <ExceptionAsserter <TException> >() != null); assert.AssertParameterIsNotNull(testMethod, nameof(testMethod)); var exceptionType = typeof(TException); try { testMethod(); } catch (TException ex) { assert.AreEqual(exceptionType, ex.GetType(), WrongException, exceptionType, ex.GetType()); return(new ExceptionAsserter <TException>(ex)); } catch (Exception ex) { assert.AreEqual(exceptionType, ex.GetType(), WrongException, exceptionType, ex.GetType()); } assert.Fail(message, parameters); return(null); }
/// <summary> /// Asserts the specified test method throws an exception of any type. /// </summary> /// <param name="assert">The extended <see cref="Asserter"/>.</param> /// <param name="testMethod">The <see cref="Action"/> representing the test method.</param> /// <param name="message">A message to display if the assertion fails. This message can be seen in the unit test results.</param> /// <param name="parameters">An array of parameters to use when formatting <paramref name="message" />.</param> /// <returns>The <see cref="ExceptionAsserter{T}">asserted exception</see>.</returns> /// <include file="examples.xml" path="Types/Type[@name='ExceptionExtensions']/Member[@name='Throws']/example[2]" /> public static ExceptionAsserter <Exception> Throws(this Asserter assert, Action testMethod, string message, params object[] parameters) { Arg.NotNull(assert, nameof(assert)); Contract.Ensures(Contract.Result <ExceptionAsserter <Exception> >() != null); assert.AssertParameterIsNotNull(testMethod, nameof(testMethod)); try { testMethod(); } catch (Exception ex) { return(new ExceptionAsserter <Exception>(ex)); } assert.Fail(message, parameters); return(default(ExceptionAsserter <Exception>)); }