PageRenderTime 52ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/Releases/1.1/FluentAssertions/ExceptionAssertions.cs

#
C# | 130 lines | 71 code | 16 blank | 43 comment | 8 complexity | a9c6e2912560791f02c6841a2bfb7f47 MD5 | raw file
  1. using System;
  2. using System.Diagnostics;
  3. namespace FluentAssertions
  4. {
  5. public static partial class FluentAssertionExtensions
  6. {
  7. [DebuggerNonUserCode]
  8. public class ExceptionAssertions<TException> : Assertions<Exception, ExceptionAssertions<TException>>
  9. where TException : Exception
  10. {
  11. internal ExceptionAssertions(TException exception)
  12. {
  13. ValueOf = exception;
  14. }
  15. /// <summary>
  16. /// Gets the exception object of the exception thrown.
  17. /// </summary>
  18. public TException ValueOf { get; private set; }
  19. /// <summary>
  20. /// Asserts that the thrown exception has a message matching the <paramref name="expectedMessage"/>.
  21. /// </summary>
  22. /// <param name="expectedMessage">The expected message of the exception.</param>
  23. /// <returns>An <see cref="AndConstraint"/> which can be used to chain assertions.</returns>
  24. public AndConstraint<ExceptionAssertions<TException>> WithMessage(string expectedMessage)
  25. {
  26. return WithMessage(expectedMessage, null, null);
  27. }
  28. /// <summary>
  29. /// Asserts that the thrown exception has a message matching the <paramref name="expectedMessage"/>.
  30. /// </summary>
  31. /// <param name="expectedMessage">The expected message of the exception.</param>
  32. /// <param name="reason">
  33. /// The reason why the message of the exception should match the <paramref name="expectedMessage"/>.
  34. /// </param>
  35. /// <param name="reasonParameters">The parameters used when formatting the <paramref name="reason"/>.</param>
  36. /// <returns>An <see cref="AndConstraint"/> which can be used to chain assertions.</returns>
  37. public AndConstraint<ExceptionAssertions<TException>> WithMessage(string expectedMessage, string reason,
  38. params object[] reasonParameters)
  39. {
  40. VerifyThat((ValueOf != null),
  41. "Expected exception with message <{0}>{2}, but no exception was thrown.",
  42. expectedMessage, null, reason, reasonParameters);
  43. VerifyThat(ValueOf.Message == expectedMessage,
  44. "Expected exception with message <{0}>{2}, but found <{1}>.",
  45. expectedMessage, ValueOf.Message, reason);
  46. return new AndConstraint<ExceptionAssertions<TException>>(this);
  47. }
  48. /// <summary>
  49. /// Asserts that the thrown exception contains an inner exception of type <typeparamref name="TInnerException"/>.
  50. /// </summary>
  51. /// <typeparam name="TInnerException">The expected type of the inner exception.</typeparam>
  52. /// <returns>An <see cref="AndConstraint"/> which can be used to chain assertions.</returns>
  53. public AndConstraint<ExceptionAssertions<TException>> WithInnerException<TInnerException>()
  54. {
  55. return WithInnerException<TInnerException>(null, null);
  56. }
  57. /// <summary>
  58. /// Asserts that the thrown exception contains an inner exception of type <typeparamref name="TInnerException"/>.
  59. /// </summary>
  60. /// <typeparam name="TInnerException">The expected type of the inner exception.</typeparam>
  61. /// <param name="reason">The reason why the inner exception should be of the supplied type.</param>
  62. /// <param name="reasonParameters">The parameters used when formatting the <paramref name="reason"/>.</param>
  63. /// <returns>An <see cref="AndConstraint"/> which can be used to chain assertions.</returns>
  64. public AndConstraint<ExceptionAssertions<TException>> WithInnerException<TInnerException>(string reason,
  65. params object[] reasonParameters)
  66. {
  67. VerifyThat(ValueOf != null, "Expected inner exception <{0}>{2}, but no exception was thrown.",
  68. typeof(TInnerException), null, reason, reasonParameters);
  69. VerifyThat(ValueOf.InnerException != null,
  70. "Expected inner exception <{0}>{2}, but the thrown exception has no inner exception.",
  71. typeof(TInnerException), null, reason, reasonParameters);
  72. VerifyThat((ValueOf.InnerException.GetType() == typeof(TInnerException)),
  73. "Expected inner exception <{0}>{2}, but found <{1}>.",
  74. typeof(TInnerException),
  75. ValueOf.InnerException.GetType(),
  76. reason, reasonParameters);
  77. return new AndConstraint<ExceptionAssertions<TException>>(this);
  78. }
  79. /// <summary>
  80. /// Asserts that the thrown exception contains an inner exception with the <paramref name="expectedInnerMessage"/>.
  81. /// </summary>
  82. /// <param name="expectedInnerMessage">The expected message of the inner exception.</param>
  83. /// <returns>An <see cref="AndConstraint"/> which can be used to chain assertions.</returns>
  84. public AndConstraint<ExceptionAssertions<TException>> WithInnerMessage(string expectedInnerMessage)
  85. {
  86. return WithInnerMessage(expectedInnerMessage, null, null);
  87. }
  88. /// <summary>
  89. /// Asserts that the thrown exception contains an inner exception with the <paramref name="expectedInnerMessage"/>.
  90. /// </summary>
  91. /// <param name="expectedInnerMessage">The expected message of the inner exception.</param>
  92. /// <param name="reason">
  93. /// The reason why the message of the inner exception should match <paramref name="expectedInnerMessage"/>.
  94. /// </param>
  95. /// <param name="reasonParameters">The parameters used when formatting the <paramref name="reason"/>.</param>
  96. /// <returns>An <see cref="AndConstraint"/> which can be used to chain assertions.</returns>
  97. public AndConstraint<ExceptionAssertions<TException>> WithInnerMessage(string expectedInnerMessage, string reason,
  98. params object[] reasonParameters)
  99. {
  100. VerifyThat(ValueOf != null, "Expected exception{2}, but no exception was thrown.",
  101. null, null, reason, reasonParameters);
  102. VerifyThat(ValueOf.InnerException != null,
  103. "Expected exception{2}, but the thrown exception has no inner exception.",
  104. null, null, reason, reasonParameters);
  105. VerifyThat((ValueOf.InnerException.Message == expectedInnerMessage),
  106. "Expected inner exception with message <{0}>{2}, but found <{1}>.",
  107. expectedInnerMessage,
  108. ValueOf.InnerException.Message,
  109. reason, reasonParameters);
  110. return new AndConstraint<ExceptionAssertions<TException>>(this);
  111. }
  112. }
  113. }
  114. }