|
| 1 | +using FluentAssertions.Execution; |
| 2 | +using FluentAssertions.Primitives; |
| 3 | +using Microsoft.AspNetCore.Mvc; |
| 4 | +using Microsoft.AspNetCore.Mvc.Infrastructure; |
| 5 | +using System.Diagnostics; |
| 6 | + |
| 7 | +namespace FluentAssertions.AspNetCore.Mvc |
| 8 | +{ |
| 9 | + /// <summary> |
| 10 | + /// Contains a number of methods to assert that an <see cref="ActionResult"/> is in the expected state. |
| 11 | + /// </summary> |
| 12 | + [DebuggerNonUserCode] |
| 13 | + public class ActionResultAssertions<TValue> : ReferenceTypeAssertions<ActionResult<TValue>, ActionResultAssertions<TValue>> |
| 14 | + { |
| 15 | + #region Public Constructors |
| 16 | + |
| 17 | + /// <summary> |
| 18 | + /// Initializes a new instance of the <see cref="ActionResultAssertions{TValue}" /> class. |
| 19 | + /// </summary> |
| 20 | + public ActionResultAssertions(ActionResult<TValue> subject) |
| 21 | + { |
| 22 | + Subject = subject; |
| 23 | + } |
| 24 | + |
| 25 | + #endregion Public Constructors |
| 26 | + |
| 27 | + #region Protected Properties |
| 28 | + |
| 29 | + /// <inheritdoc /> |
| 30 | + protected override string Identifier { get; } = $"ActionResult<{typeof(TValue).Name}>"; |
| 31 | + |
| 32 | + #endregion Protected Properties |
| 33 | + |
| 34 | + #region Public Properties |
| 35 | + |
| 36 | + /// <summary> |
| 37 | + /// Gets the <see cref="ActionResult{TValue}.Result"/> of the Subject. |
| 38 | + /// </summary> |
| 39 | + public ActionResult Result => Subject.Result; |
| 40 | + |
| 41 | + /// <summary> |
| 42 | + /// Gets the <see cref="ActionResult{TValue}.Value"/> of the Subject. |
| 43 | + /// </summary> |
| 44 | + public TValue Value => Subject.Value; |
| 45 | + |
| 46 | + #endregion Public Properties |
| 47 | + |
| 48 | + #region Public Methods |
| 49 | + |
| 50 | + /// <summary> |
| 51 | + /// Asserts that the <see cref="ActionResult{TValue}.Result"/> is type of <typeparamref name="TActionResult"/>. |
| 52 | + /// </summary> |
| 53 | + /// <param name="reason"> |
| 54 | + /// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion |
| 55 | + /// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically. |
| 56 | + /// </param> |
| 57 | + /// <param name="reasonArgs"> |
| 58 | + /// Zero or more objects to format using the placeholders in <paramref name="reason"/>. |
| 59 | + /// </param> |
| 60 | + /// <returns> |
| 61 | + /// An <see cref="AndWhichConstraint{TParentConstraint, TMatchedElement}"/> where the Which contains |
| 62 | + /// the result of Result converted to <typeparamref name="TActionResult"/>. |
| 63 | + /// </returns> |
| 64 | + [CustomAssertion] |
| 65 | + public AndWhichConstraint<ActionResultAssertions<TValue>, TActionResult> BeConvertibleTo<TActionResult>( |
| 66 | + string reason = "", params object[] reasonArgs) |
| 67 | + where TActionResult : ActionResult |
| 68 | + { |
| 69 | + var convertResult = ((IConvertToActionResult)Subject).Convert(); |
| 70 | + Execute.Assertion |
| 71 | + .BecauseOf(reason, reasonArgs) |
| 72 | + .ForCondition(convertResult != null) |
| 73 | + .FailWith(FailureMessages.ConvertibleActionFailMessage, typeof(TActionResult), null); |
| 74 | + |
| 75 | + Execute.Assertion |
| 76 | + .BecauseOf(reason, reasonArgs) |
| 77 | + .ForCondition(convertResult.GetType() == typeof(TActionResult)) |
| 78 | + .FailWith(FailureMessages.ConvertibleActionFailMessage, typeof(TActionResult), convertResult.GetType()); |
| 79 | + |
| 80 | + return new AndWhichConstraint<ActionResultAssertions<TValue>, TActionResult>(this, (TActionResult)convertResult); |
| 81 | + } |
| 82 | + } |
| 83 | + #endregion Public Methods |
| 84 | +} |
0 commit comments