/Source/FakeItEasy.Tests/TestHelpers/ObjectAssertionsExtensions.cs
C# | 45 lines | 27 code | 4 blank | 14 comment | 0 complexity | bedc62e0f0d0cb0da617acc2b28feb4f MD5 | raw file
- namespace FakeItEasy.Tests.TestHelpers
- {
- using System;
- using System.Diagnostics.CodeAnalysis;
- using FluentAssertions.Primitives;
- using FluentAssertions.Specialized;
-
- /// <summary>
- /// Extends the capabilities of <see cref="ObjectAssertions"/>.
- /// </summary>
- public static class ObjectAssertionsExtensions
- {
- /// <summary>
- /// Verifies that the passed-in assertion refers to a <see cref="ReferenceTypeAssertions.Subject"/>
- /// that is not null and matches the expected type.
- /// </summary>
- /// <typeparam name="TExpectedException">The expected exception type.</typeparam>
- /// <param name="assertion">A FluentAssertions assertion that has been initiated on a subject.</param>
- /// <returns>An <see cref="ExceptionAssertions{T}"/> object that can be further used to assert against the subject.</returns>
- [SuppressMessage("Microsoft.Naming", "CA1702:CompoundWordsShouldBeCasedCorrectly", MessageId = "BeAn", Justification = "Refers to the two words 'be an'")]
- public static ExceptionAssertions<TExpectedException> BeAnExceptionOfType<TExpectedException>(this ReferenceTypeAssertions<object, ObjectAssertions> assertion) where TExpectedException : Exception
- {
- Guard.AgainstNull(assertion, "assertion");
-
- assertion
- .NotBeNull().And
- .BeOfType<TExpectedException>();
-
- var exception = (TExpectedException)assertion.Subject;
- return new MyExceptionAssertions<TExpectedException>(exception);
- }
-
- /// <summary>
- /// A convenient extension so we can access the <see cref="ExceptionAssertions"/> constructor.
- /// </summary>
- /// <typeparam name="TException">The type of exception to require.</typeparam>
- private class MyExceptionAssertions<TException> : ExceptionAssertions<TException> where TException : Exception
- {
- public MyExceptionAssertions(TException exception)
- : base(new[] { exception })
- {
- }
- }
- }
- }