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

/Source/FakeItEasy.Tests/TestHelpers/ObjectAssertionsExtensions.cs

https://github.com/ambygit/FakeItEasy
C# | 45 lines | 27 code | 4 blank | 14 comment | 0 complexity | bedc62e0f0d0cb0da617acc2b28feb4f MD5 | raw file
  1. namespace FakeItEasy.Tests.TestHelpers
  2. {
  3. using System;
  4. using System.Diagnostics.CodeAnalysis;
  5. using FluentAssertions.Primitives;
  6. using FluentAssertions.Specialized;
  7. /// <summary>
  8. /// Extends the capabilities of <see cref="ObjectAssertions"/>.
  9. /// </summary>
  10. public static class ObjectAssertionsExtensions
  11. {
  12. /// <summary>
  13. /// Verifies that the passed-in assertion refers to a <see cref="ReferenceTypeAssertions.Subject"/>
  14. /// that is not null and matches the expected type.
  15. /// </summary>
  16. /// <typeparam name="TExpectedException">The expected exception type.</typeparam>
  17. /// <param name="assertion">A FluentAssertions assertion that has been initiated on a subject.</param>
  18. /// <returns>An <see cref="ExceptionAssertions{T}"/> object that can be further used to assert against the subject.</returns>
  19. [SuppressMessage("Microsoft.Naming", "CA1702:CompoundWordsShouldBeCasedCorrectly", MessageId = "BeAn", Justification = "Refers to the two words 'be an'")]
  20. public static ExceptionAssertions<TExpectedException> BeAnExceptionOfType<TExpectedException>(this ReferenceTypeAssertions<object, ObjectAssertions> assertion) where TExpectedException : Exception
  21. {
  22. Guard.AgainstNull(assertion, "assertion");
  23. assertion
  24. .NotBeNull().And
  25. .BeOfType<TExpectedException>();
  26. var exception = (TExpectedException)assertion.Subject;
  27. return new MyExceptionAssertions<TExpectedException>(exception);
  28. }
  29. /// <summary>
  30. /// A convenient extension so we can access the <see cref="ExceptionAssertions"/> constructor.
  31. /// </summary>
  32. /// <typeparam name="TException">The type of exception to require.</typeparam>
  33. private class MyExceptionAssertions<TException> : ExceptionAssertions<TException> where TException : Exception
  34. {
  35. public MyExceptionAssertions(TException exception)
  36. : base(new[] { exception })
  37. {
  38. }
  39. }
  40. }
  41. }