diff --git a/src/FluentAssertions.AspNetCore.Mvc/AcceptedAtActionResultAssertions.cs b/src/FluentAssertions.AspNetCore.Mvc/AcceptedAtActionResultAssertions.cs index b05f417..c935e6f 100644 --- a/src/FluentAssertions.AspNetCore.Mvc/AcceptedAtActionResultAssertions.cs +++ b/src/FluentAssertions.AspNetCore.Mvc/AcceptedAtActionResultAssertions.cs @@ -1,8 +1,7 @@ -using System; -using System.Diagnostics; -using FluentAssertions.Execution; -using FluentAssertions.Primitives; +using FluentAssertions.Execution; using Microsoft.AspNetCore.Mvc; +using System; +using System.Diagnostics; namespace FluentAssertions.AspNetCore.Mvc { @@ -10,7 +9,7 @@ namespace FluentAssertions.AspNetCore.Mvc /// Contains a number of methods to assert that a is in the expected state. /// [DebuggerNonUserCode] - public class AcceptedAtActionResultAssertions : ObjectAssertions + public class AcceptedAtActionResultAssertions : ObjectResultAssertionsBase { /// /// Initializes a new instance of the class. @@ -30,7 +29,7 @@ public AcceptedAtActionResultAssertions(AcceptedAtActionResult subject) : base(s /// public AcceptedAtActionResultAssertions WithActionName(string expectedActionName, string reason = "", params object[] reasonArgs) { - string actualActionName = (Subject as AcceptedAtActionResult)?.ActionName; + string actualActionName = ObjectResultSubject.ActionName; Execute.Assertion .ForCondition(string.Equals(actualActionName, expectedActionName, StringComparison.OrdinalIgnoreCase)) @@ -54,7 +53,7 @@ public AcceptedAtActionResultAssertions WithActionName(string expectedActionName /// public AcceptedAtActionResultAssertions WithControllerName(string expectedControllerName, string reason = "", params object[] reasonArgs) { - string actualControllerName = (Subject as AcceptedAtActionResult)?.ControllerName; + string actualControllerName = ObjectResultSubject.ControllerName; Execute.Assertion .ForCondition(string.Equals(actualControllerName, expectedControllerName, StringComparison.OrdinalIgnoreCase)) @@ -79,35 +78,12 @@ public AcceptedAtActionResultAssertions WithControllerName(string expectedContro /// public AcceptedAtActionResultAssertions WithRouteValue(string key, object expectedValue, string reason = "", params object[] reasonArgs) { - var subjectTyped = Subject as AcceptedAtActionResult; + var actualRouteValues = ObjectResultSubject.RouteValues; - AssertionHelpers.AssertStringObjectDictionary(subjectTyped.RouteValues, + AssertionHelpers.AssertStringObjectDictionary(actualRouteValues, "AcceptedAtActionResult.RouteValues", key, expectedValue, reason, reasonArgs); return this; } - - /// - /// Asserts the value is of the expected type. - /// - /// The expected type. - /// The typed value. - public TValue ValueAs() - { - var subjectTyped = Subject as AcceptedAtActionResult; - var value = subjectTyped.Value; - - if (value == null) - Execute.Assertion - .WithDefaultIdentifier("AcceptedAtActionResult.Value") - .FailWith(FailureMessages.CommonNullWasSuppliedFailMessage, typeof(TValue)); - - Execute.Assertion - .ForCondition(value is TValue) - .WithDefaultIdentifier("AcceptedAtActionResult.Value") - .FailWith(FailureMessages.CommonTypeFailMessage, typeof(TValue), value.GetType()); - - return (TValue)value; - } } } diff --git a/src/FluentAssertions.AspNetCore.Mvc/AcceptedAtRouteResultAssertions.cs b/src/FluentAssertions.AspNetCore.Mvc/AcceptedAtRouteResultAssertions.cs index 1b4d9ef..e02e7ac 100644 --- a/src/FluentAssertions.AspNetCore.Mvc/AcceptedAtRouteResultAssertions.cs +++ b/src/FluentAssertions.AspNetCore.Mvc/AcceptedAtRouteResultAssertions.cs @@ -1,8 +1,7 @@ -using System; -using System.Diagnostics; -using FluentAssertions.Execution; -using FluentAssertions.Primitives; +using FluentAssertions.Execution; using Microsoft.AspNetCore.Mvc; +using System; +using System.Diagnostics; namespace FluentAssertions.AspNetCore.Mvc { @@ -10,7 +9,7 @@ namespace FluentAssertions.AspNetCore.Mvc /// Contains a number of methods to assert that a is in the expected state. /// [DebuggerNonUserCode] - public class AcceptedAtRouteResultAssertions : ObjectAssertions + public class AcceptedAtRouteResultAssertions : ObjectResultAssertionsBase { /// /// Initializes a new instance of the class. @@ -30,13 +29,13 @@ public AcceptedAtRouteResultAssertions(AcceptedAtRouteResult subject) : base(sub /// public AcceptedAtRouteResultAssertions WithRouteName(string expectedRouteName, string reason = "", params object[] reasonArgs) { - var subjectTyped = Subject as AcceptedAtRouteResult; + var actualRouteName = ObjectResultSubject.RouteName; Execute.Assertion .BecauseOf(reason, reasonArgs) - .ForCondition(string.Equals(expectedRouteName, subjectTyped.RouteName, StringComparison.OrdinalIgnoreCase)) + .ForCondition(string.Equals(expectedRouteName, actualRouteName, StringComparison.OrdinalIgnoreCase)) .WithDefaultIdentifier("AcceptedAtRouteResult.RouteName") - .FailWith(FailureMessages.CommonFailMessage, expectedRouteName, subjectTyped.RouteName); + .FailWith(FailureMessages.CommonFailMessage, expectedRouteName, actualRouteName); return this; } @@ -55,35 +54,12 @@ public AcceptedAtRouteResultAssertions WithRouteName(string expectedRouteName, s /// public AcceptedAtRouteResultAssertions WithRouteValue(string key, object expectedValue, string reason = "", params object[] reasonArgs) { - var subjectTyped = Subject as AcceptedAtRouteResult; + var actualRouteValues = ObjectResultSubject.RouteValues; - AssertionHelpers.AssertStringObjectDictionary(subjectTyped.RouteValues, + AssertionHelpers.AssertStringObjectDictionary(actualRouteValues, "AcceptedAtRouteResult.RouteValues", key, expectedValue, reason, reasonArgs); return this; } - - /// - /// Asserts the value is of the expected type. - /// - /// The expected type. - /// The typed value. - public TValue ValueAs() - { - var subjectTyped = Subject as AcceptedAtRouteResult; - var value = subjectTyped.Value; - - if (value == null) - Execute.Assertion - .WithDefaultIdentifier("AcceptedAtRouteResult.Value") - .FailWith(FailureMessages.CommonNullWasSuppliedFailMessage, typeof(TValue)); - - Execute.Assertion - .ForCondition(value is TValue) - .WithDefaultIdentifier("AcceptedAtRouteResult.Value") - .FailWith(FailureMessages.CommonTypeFailMessage, typeof(TValue), value.GetType()); - - return (TValue)value; - } } } diff --git a/src/FluentAssertions.AspNetCore.Mvc/AcceptedResultAssertions.cs b/src/FluentAssertions.AspNetCore.Mvc/AcceptedResultAssertions.cs index 93c7881..8cdc038 100644 --- a/src/FluentAssertions.AspNetCore.Mvc/AcceptedResultAssertions.cs +++ b/src/FluentAssertions.AspNetCore.Mvc/AcceptedResultAssertions.cs @@ -1,8 +1,7 @@ -using System; -using System.Diagnostics; -using FluentAssertions.Execution; -using FluentAssertions.Primitives; +using FluentAssertions.Execution; using Microsoft.AspNetCore.Mvc; +using System; +using System.Diagnostics; namespace FluentAssertions.AspNetCore.Mvc { @@ -10,7 +9,7 @@ namespace FluentAssertions.AspNetCore.Mvc /// Contains a number of methods to assert that a is in the expected state. /// [DebuggerNonUserCode] - public class AcceptedResultAssertions : ObjectAssertions + public class AcceptedResultAssertions : ObjectResultAssertionsBase { #region Public Constructors @@ -26,42 +25,13 @@ public AcceptedResultAssertions(AcceptedResult subject) : base(subject) #region Public Properties /// - /// The value on the AcceptedResult + /// The location on the AcceptedResult. /// - public object Value => AcceptedResultSubject.Value; - - public string Location => AcceptedResultSubject.Location; - - #endregion - - #region Private Properties - private AcceptedResult AcceptedResultSubject => (AcceptedResult)Subject; + public string Location => ObjectResultSubject.Location; #endregion #region Public Methods - /// - /// Asserts the value is of the expected type. - /// - /// The expected type. - /// The typed value. - public TValue ValueAs() - { - var value = AcceptedResultSubject.Value; - - if (value == null) - Execute.Assertion - .WithDefaultIdentifier("AcceptedResultAssertions.Value") - .FailWith(FailureMessages.CommonNullWasSuppliedFailMessage, typeof(TValue)); - - Execute.Assertion - .ForCondition(value is TValue) - .WithDefaultIdentifier("AcceptedResultAssertions.Value") - .FailWith(FailureMessages.CommonTypeFailMessage, typeof(TValue), value.GetType()); - - return (TValue)value; - } - /// /// Asserts the uri has the expected value. /// @@ -83,7 +53,7 @@ public AcceptedResultAssertions WithUri(Uri uri, string reason = "", params obje Execute.Assertion .BecauseOf(reason, reasonArgs) .ForCondition(expectedUri == Location) - .WithDefaultIdentifier("AcceptedResultAssertions.Uri") + .WithDefaultIdentifier("AcceptedResult.Uri") .FailWith(FailureMessages.CommonFailMessage, expectedUri, Location); return this; @@ -108,7 +78,7 @@ public AcceptedResultAssertions WithUri(string uri, string reason = "", params o Execute.Assertion .BecauseOf(reason, reasonArgs) .ForCondition(uri == Location) - .WithDefaultIdentifier("AcceptedResultAssertions.Uri") + .WithDefaultIdentifier("AcceptedResult.Uri") .FailWith(FailureMessages.CommonFailMessage, uri, Location); return this; diff --git a/src/FluentAssertions.AspNetCore.Mvc/ActionResultAssertions.cs b/src/FluentAssertions.AspNetCore.Mvc/ActionResultAssertions.cs index f893caa..47466f1 100644 --- a/src/FluentAssertions.AspNetCore.Mvc/ActionResultAssertions.cs +++ b/src/FluentAssertions.AspNetCore.Mvc/ActionResultAssertions.cs @@ -24,7 +24,9 @@ public ActionResultAssertions(IActionResult subject) : base(subject) #endregion Public Constructors #region Protected Properties - + /// + /// + /// protected override string Identifier => "ActionResult"; #endregion Protected Properties @@ -325,6 +327,27 @@ public StatusCodeResultAssertions BeStatusCodeResult(string reason = "", params return new StatusCodeResultAssertions(Subject as StatusCodeResult); } + /// + /// Asserts that the subject is a . + /// + /// + /// A formatted phrase as is supported by explaining why the assertion + /// is needed. If the phrase does not start with the word because, it is prepended automatically. + /// + /// + /// Zero or more objects to format using the placeholders in . + /// + [CustomAssertion] + public ObjectResultAssertions BeObjectResult(string reason = "", params object[] reasonArgs) + { + Execute.Assertion + .BecauseOf(reason, reasonArgs) + .ForCondition(Subject is ObjectResult) + .FailWith(FailureMessages.CommonTypeFailMessage, typeof(ObjectResult), Subject.GetType()); + + return new ObjectResultAssertions(Subject as ObjectResult); + } + /// /// Asserts that the subject is an . /// @@ -362,7 +385,7 @@ public OkObjectResultAssertions BeOkObjectResult(string reason = "", params obje Execute.Assertion .BecauseOf(reason, reasonArgs) .ForCondition(Subject is OkObjectResult) - .FailWith(FailureMessages.CommonTypeFailMessage, typeof(OkObjectResult).Name, Subject.GetType().Name); + .FailWith(FailureMessages.CommonTypeFailMessage, typeof(OkObjectResult), Subject.GetType()); return new OkObjectResultAssertions(Subject as OkObjectResult); } @@ -404,7 +427,7 @@ public BadRequestObjectResultAssertions BeBadRequestObjectResult(string reason = Execute.Assertion .BecauseOf(reason, reasonArgs) .ForCondition(Subject is BadRequestObjectResult) - .FailWith(FailureMessages.CommonTypeFailMessage, typeof(BadRequestObjectResult).Name, Subject.GetType().Name); + .FailWith(FailureMessages.CommonTypeFailMessage, typeof(BadRequestObjectResult), Subject.GetType()); return new BadRequestObjectResultAssertions(Subject as BadRequestObjectResult); } @@ -425,7 +448,7 @@ public CreatedResultAssertions BeCreatedResult(string reason = "", params object Execute.Assertion .BecauseOf(reason, reasonArgs) .ForCondition(Subject is CreatedResult) - .FailWith(FailureMessages.CommonTypeFailMessage, typeof(CreatedResult).Name, Subject.GetType().Name); + .FailWith(FailureMessages.CommonTypeFailMessage, typeof(CreatedResult), Subject.GetType()); return new CreatedResultAssertions(Subject as CreatedResult); } @@ -488,7 +511,7 @@ public ForbidResultAssertions BeForbidResult(string reason = "", params object[] Execute.Assertion .BecauseOf(reason, reasonArgs) .ForCondition(Subject is ForbidResult) - .FailWith(FailureMessages.CommonTypeFailMessage, typeof(ForbidResult).Name, Subject.GetType().Name); + .FailWith(FailureMessages.CommonTypeFailMessage, typeof(ForbidResult), Subject.GetType()); return new ForbidResultAssertions(Subject as ForbidResult); } @@ -551,7 +574,7 @@ public NotFoundObjectResultAssertions BeNotFoundObjectResult(string reason = "", Execute.Assertion .BecauseOf(reason, reasonArgs) .ForCondition(Subject is NotFoundObjectResult) - .FailWith(FailureMessages.CommonTypeFailMessage, typeof(NotFoundObjectResult).Name, Subject.GetType().Name); + .FailWith(FailureMessages.CommonTypeFailMessage, typeof(NotFoundObjectResult), Subject.GetType()); return new NotFoundObjectResultAssertions(Subject as NotFoundObjectResult); } @@ -593,7 +616,7 @@ public SignInResultAssertions BeSignInResult(string reason = "", params object[] Execute.Assertion .BecauseOf(reason, reasonArgs) .ForCondition(Subject is SignInResult) - .FailWith(FailureMessages.CommonTypeFailMessage, typeof(SignInResult).Name, Subject.GetType().Name); + .FailWith(FailureMessages.CommonTypeFailMessage, typeof(SignInResult), Subject.GetType()); return new SignInResultAssertions(Subject as SignInResult); } @@ -656,7 +679,7 @@ public AcceptedAtActionResultAssertions BeAcceptedAtActionResult(string reason = Execute.Assertion .BecauseOf(reason, reasonArgs) .ForCondition(Subject is AcceptedAtActionResult) - .FailWith(FailureMessages.CommonTypeFailMessage, typeof(AcceptedAtActionResult).Name, Subject.GetType().Name); + .FailWith(FailureMessages.CommonTypeFailMessage, typeof(AcceptedAtActionResult), Subject.GetType()); return new AcceptedAtActionResultAssertions(Subject as AcceptedAtActionResult); } @@ -677,7 +700,7 @@ public AcceptedAtRouteResultAssertions BeAcceptedAtRouteResult(string reason = " Execute.Assertion .BecauseOf(reason, reasonArgs) .ForCondition(Subject is AcceptedAtRouteResult) - .FailWith(FailureMessages.CommonTypeFailMessage, typeof(AcceptedAtRouteResult).Name, Subject.GetType().Name); + .FailWith(FailureMessages.CommonTypeFailMessage, typeof(AcceptedAtRouteResult), Subject.GetType()); return new AcceptedAtRouteResultAssertions(Subject as AcceptedAtRouteResult); } @@ -698,7 +721,7 @@ public CreatedAtActionResultAssertions BeCreatedAtActionResult(string reason = " Execute.Assertion .BecauseOf(reason, reasonArgs) .ForCondition(Subject is CreatedAtActionResult) - .FailWith(FailureMessages.CommonTypeFailMessage, typeof(CreatedAtActionResult).Name, Subject.GetType().Name); + .FailWith(FailureMessages.CommonTypeFailMessage, typeof(CreatedAtActionResult), Subject.GetType()); return new CreatedAtActionResultAssertions(Subject as CreatedAtActionResult); } @@ -719,7 +742,7 @@ public CreatedAtRouteResultAssertions BeCreatedAtRouteResult(string reason = "", Execute.Assertion .BecauseOf(reason, reasonArgs) .ForCondition(Subject is CreatedAtRouteResult) - .FailWith(FailureMessages.CommonTypeFailMessage, typeof(CreatedAtRouteResult).Name, Subject.GetType().Name); + .FailWith(FailureMessages.CommonTypeFailMessage, typeof(CreatedAtRouteResult), Subject.GetType()); return new CreatedAtRouteResultAssertions(Subject as CreatedAtRouteResult); } diff --git a/src/FluentAssertions.AspNetCore.Mvc/ChallengeResultAssertions.cs b/src/FluentAssertions.AspNetCore.Mvc/ChallengeResultAssertions.cs index e8fc98b..f85a156 100644 --- a/src/FluentAssertions.AspNetCore.Mvc/ChallengeResultAssertions.cs +++ b/src/FluentAssertions.AspNetCore.Mvc/ChallengeResultAssertions.cs @@ -29,13 +29,44 @@ public ChallengeResultAssertions(object subject) : base(subject) #endregion #region Public Properties + /// + /// The that belongs to the ChallengeResult. + /// public AuthenticationProperties AuthenticationProperties => ChallengeResultSubject.Properties; + + /// + /// The that belongs to the ChallengeResult. + /// public IDictionary Items => ChallengeResultSubject.Properties?.Items; + + /// + /// The that belongs to the ChallengeResult. + /// public bool IsPersistent => ChallengeResultSubject.Properties?.IsPersistent ?? false; + + /// + /// The that belongs to the ChallengeResult. + /// public string RedirectUri => ChallengeResultSubject.Properties?.RedirectUri; + + /// + /// The that belongs to the ChallengeResult. + /// public DateTimeOffset? IssuedUtc => ChallengeResultSubject.Properties?.IssuedUtc; + + /// + /// The that belongs to the ChallengeResult. + /// public DateTimeOffset? ExpiresUtc => ChallengeResultSubject.Properties?.ExpiresUtc; + + /// + /// The that belongs to the ChallengeResult. + /// public bool? AllowRefresh => ChallengeResultSubject.Properties?.AllowRefresh; + + /// + /// The that belongs to the ChallengeResult. + /// public IList AuthenticationSchemes => ChallengeResultSubject.AuthenticationSchemes; #endregion diff --git a/src/FluentAssertions.AspNetCore.Mvc/CreatedAtActionResultAssertions.cs b/src/FluentAssertions.AspNetCore.Mvc/CreatedAtActionResultAssertions.cs index 2ef6c83..dfda3c8 100644 --- a/src/FluentAssertions.AspNetCore.Mvc/CreatedAtActionResultAssertions.cs +++ b/src/FluentAssertions.AspNetCore.Mvc/CreatedAtActionResultAssertions.cs @@ -1,8 +1,7 @@ -using System; -using System.Diagnostics; -using FluentAssertions.Execution; -using FluentAssertions.Primitives; +using FluentAssertions.Execution; using Microsoft.AspNetCore.Mvc; +using System; +using System.Diagnostics; namespace FluentAssertions.AspNetCore.Mvc { @@ -10,7 +9,7 @@ namespace FluentAssertions.AspNetCore.Mvc /// Contains a number of methods to assert that a is in the expected state. /// [DebuggerNonUserCode] - public class CreatedAtActionResultAssertions : ObjectAssertions + public class CreatedAtActionResultAssertions : ObjectResultAssertionsBase { /// /// Initializes a new instance of the class. @@ -30,7 +29,7 @@ public CreatedAtActionResultAssertions(CreatedAtActionResult subject) : base(sub /// public CreatedAtActionResultAssertions WithActionName(string expectedActionName, string reason = "", params object[] reasonArgs) { - string actualActionName = (Subject as CreatedAtActionResult)?.ActionName; + string actualActionName = ObjectResultSubject.ActionName; Execute.Assertion .ForCondition(string.Equals(actualActionName, expectedActionName, StringComparison.OrdinalIgnoreCase)) @@ -54,7 +53,7 @@ public CreatedAtActionResultAssertions WithActionName(string expectedActionName, /// public CreatedAtActionResultAssertions WithControllerName(string expectedControllerName, string reason = "", params object[] reasonArgs) { - string actualControllerName = (Subject as CreatedAtActionResult)?.ControllerName; + string actualControllerName = ObjectResultSubject.ControllerName; Execute.Assertion .ForCondition(string.Equals(actualControllerName, expectedControllerName, StringComparison.OrdinalIgnoreCase)) @@ -79,35 +78,12 @@ public CreatedAtActionResultAssertions WithControllerName(string expectedControl /// public CreatedAtActionResultAssertions WithRouteValue(string key, object expectedValue, string reason = "", params object[] reasonArgs) { - var subjectTyped = Subject as CreatedAtActionResult; + var actualRouteValues = ObjectResultSubject.RouteValues; - AssertionHelpers.AssertStringObjectDictionary(subjectTyped.RouteValues, "CreatedAtActionResult.RouteValues", + AssertionHelpers.AssertStringObjectDictionary(actualRouteValues, "CreatedAtActionResult.RouteValues", key, expectedValue, reason, reasonArgs); return this; } - - /// - /// Asserts the value is of the expected type. - /// - /// The expected type. - /// The typed value. - public TValue ValueAs() - { - var subjectTyped = Subject as CreatedAtActionResult; - var value = subjectTyped.Value; - - if (value == null) - Execute.Assertion - .WithDefaultIdentifier("CreatedAtActionResult.Value") - .FailWith(FailureMessages.CommonNullWasSuppliedFailMessage, typeof(TValue)); - - Execute.Assertion - .ForCondition(value is TValue) - .WithDefaultIdentifier("CreatedAtActionResult.Value") - .FailWith(FailureMessages.CommonTypeFailMessage, typeof(TValue), value.GetType()); - - return (TValue)value; - } } } diff --git a/src/FluentAssertions.AspNetCore.Mvc/CreatedAtRouteResultAssertions.cs b/src/FluentAssertions.AspNetCore.Mvc/CreatedAtRouteResultAssertions.cs index c3e0fcf..afed025 100644 --- a/src/FluentAssertions.AspNetCore.Mvc/CreatedAtRouteResultAssertions.cs +++ b/src/FluentAssertions.AspNetCore.Mvc/CreatedAtRouteResultAssertions.cs @@ -1,8 +1,7 @@ -using System; -using System.Diagnostics; -using FluentAssertions.Execution; -using FluentAssertions.Primitives; +using FluentAssertions.Execution; using Microsoft.AspNetCore.Mvc; +using System; +using System.Diagnostics; namespace FluentAssertions.AspNetCore.Mvc { @@ -10,7 +9,7 @@ namespace FluentAssertions.AspNetCore.Mvc /// Contains a number of methods to assert that a is in the expected state. /// [DebuggerNonUserCode] - public class CreatedAtRouteResultAssertions : ObjectAssertions + public class CreatedAtRouteResultAssertions : ObjectResultAssertionsBase { /// /// Initializes a new instance of the class. @@ -62,28 +61,5 @@ public CreatedAtRouteResultAssertions WithRouteValue(string key, object expected return this; } - - /// - /// Asserts the value is of the expected type. - /// - /// The expected type. - /// The typed value. - public TValue ValueAs() - { - var subjectTyped = Subject as CreatedAtRouteResult; - var value = subjectTyped.Value; - - if (value == null) - Execute.Assertion - .WithDefaultIdentifier("CreatedAtRouteResult.Value") - .FailWith(FailureMessages.CommonNullWasSuppliedFailMessage, typeof(TValue)); - - Execute.Assertion - .ForCondition(value is TValue) - .WithDefaultIdentifier("CreatedAtRouteResult.Value") - .FailWith(FailureMessages.CommonTypeFailMessage, typeof(TValue), value.GetType()); - - return (TValue)value; - } } } diff --git a/src/FluentAssertions.AspNetCore.Mvc/CreatedResultAssertions.cs b/src/FluentAssertions.AspNetCore.Mvc/CreatedResultAssertions.cs index 22e21b5..7f98520 100644 --- a/src/FluentAssertions.AspNetCore.Mvc/CreatedResultAssertions.cs +++ b/src/FluentAssertions.AspNetCore.Mvc/CreatedResultAssertions.cs @@ -1,8 +1,7 @@ -using System; -using System.Diagnostics; -using FluentAssertions.Execution; -using FluentAssertions.Primitives; +using FluentAssertions.Execution; using Microsoft.AspNetCore.Mvc; +using System; +using System.Diagnostics; namespace FluentAssertions.AspNetCore.Mvc { @@ -10,7 +9,7 @@ namespace FluentAssertions.AspNetCore.Mvc /// Contains a number of methods to assert that a is in the expected state. /// [DebuggerNonUserCode] - public class CreatedResultAssertions : ObjectAssertions + public class CreatedResultAssertions : ObjectResultAssertionsBase { #region Public Constructors @@ -26,42 +25,13 @@ public CreatedResultAssertions(CreatedResult subject) : base(subject) #region Public Properties /// - /// The value on the CreatedResult + /// The location on the CreatedResult. /// - public object Value => CreatedResultSubject.Value; - - public string Location => CreatedResultSubject.Location; - - #endregion - - #region Private Properties - private CreatedResult CreatedResultSubject => (CreatedResult)Subject; + public string Location => ObjectResultSubject.Location; #endregion #region Public Methods - /// - /// Asserts the value is of the expected type. - /// - /// The expected type. - /// The typed value. - public TValue ValueAs() - { - var value = CreatedResultSubject.Value; - - if (value == null) - Execute.Assertion - .WithDefaultIdentifier("CreatedResult.Value") - .FailWith(FailureMessages.CommonNullWasSuppliedFailMessage, typeof(TValue)); - - Execute.Assertion - .ForCondition(value is TValue) - .WithDefaultIdentifier("CreatedResult.Value") - .FailWith(FailureMessages.CommonTypeFailMessage, typeof(TValue), value.GetType()); - - return (TValue)value; - } - /// /// Asserts the uri has the expected value. /// diff --git a/src/FluentAssertions.AspNetCore.Mvc/FailureMessages.Designer.cs b/src/FluentAssertions.AspNetCore.Mvc/FailureMessages.Designer.cs index 7aa234b..8b79535 100644 --- a/src/FluentAssertions.AspNetCore.Mvc/FailureMessages.Designer.cs +++ b/src/FluentAssertions.AspNetCore.Mvc/FailureMessages.Designer.cs @@ -106,7 +106,7 @@ internal static string CommonNullWasSuppliedFailMessage { } /// - /// Looks up a localized string similar to Expected {context} to be of type {0} but was {1}.. + /// Looks up a localized string similar to Expected {context} to be of type {0} but was {1}{reason}.. /// internal static string CommonTypeFailMessage { get { diff --git a/src/FluentAssertions.AspNetCore.Mvc/FailureMessages.resx b/src/FluentAssertions.AspNetCore.Mvc/FailureMessages.resx index d62f4d3..986832e 100644 --- a/src/FluentAssertions.AspNetCore.Mvc/FailureMessages.resx +++ b/src/FluentAssertions.AspNetCore.Mvc/FailureMessages.resx @@ -133,7 +133,7 @@ Expected {context} to be of type {0}, but no value was supplied. - Expected {context} to be of type {0} but was {1}. + Expected {context} to be of type {0} but was {1}{reason}. Expected {context} to be ActionResult<TValue> with type {0}{reason} but was {1}. diff --git a/src/FluentAssertions.AspNetCore.Mvc/FluentAssertions.AspNetCore.Mvc.csproj b/src/FluentAssertions.AspNetCore.Mvc/FluentAssertions.AspNetCore.Mvc.csproj index df0dc4e..4222a96 100644 --- a/src/FluentAssertions.AspNetCore.Mvc/FluentAssertions.AspNetCore.Mvc.csproj +++ b/src/FluentAssertions.AspNetCore.Mvc/FluentAssertions.AspNetCore.Mvc.csproj @@ -5,12 +5,12 @@ Copyright 2018 Fluent Assertions extensions for ASP.NET Core MVC Fluent Assertions for ASP.NET Core MVC - 3.0.0 + 3.1.0 Casey Burns;Kevin Kuszyk netstandard2.0;netcoreapp3.0 FluentAssertions.AspNetCore.Mvc FluentAssertions.AspNetCore.Mvc - TDD;TDD;Fluent;Mvc;AspNetMvc;aspnetcore;aspnetcoremvc + TDD;Fluent;Mvc;AspNetMvc;aspnetcore;aspnetcoremvc See https://github.com/fluentassertions/fluentassertions.aspnetcore.mvc/releases https://github.com/fluentassertions/fluentassertions.aspnetcore.mvc https://github.com/fluentassertions/fluentassertions.aspnetcore.mvc/blob/master/license.txt diff --git a/src/FluentAssertions.AspNetCore.Mvc/ForbidResultAssertions.cs b/src/FluentAssertions.AspNetCore.Mvc/ForbidResultAssertions.cs index 99ba954..9093bd2 100644 --- a/src/FluentAssertions.AspNetCore.Mvc/ForbidResultAssertions.cs +++ b/src/FluentAssertions.AspNetCore.Mvc/ForbidResultAssertions.cs @@ -28,13 +28,45 @@ public ForbidResultAssertions(ForbidResult subject) : base(subject) #endregion #region Public Properties + + /// + /// The authentication properties on the ForbidResult. + /// public AuthenticationProperties AuthenticationProperties => ForbidResultSubject.Properties; + + /// + /// The items value in the authentication properties on the ForbidResult. + /// public IDictionary Items => ForbidResultSubject.Properties?.Items; + + /// + /// The is persistent value in the authentication properties on the ForbidResult. + /// public bool IsPersistent => ForbidResultSubject.Properties?.IsPersistent ?? false; + + /// + /// The redirect uri value in the authentication properties on the ForbidResult. + /// public string RedirectUri => ForbidResultSubject.Properties?.RedirectUri; + + /// + /// The issued utc value in the authentication properties on the ForbidResult. + /// public DateTimeOffset? IssuedUtc => ForbidResultSubject.Properties?.IssuedUtc; + + /// + /// The expires utc value in the authentication properties on the ForbidResult. + /// public DateTimeOffset? ExpiresUtc => ForbidResultSubject.Properties?.ExpiresUtc; + + /// + /// The allow refresh value in the authentication properties on the ForbidResult. + /// public bool? AllowRefresh => ForbidResultSubject.Properties?.AllowRefresh; + + /// + /// The authentication schemes value on the ForbidResult. + /// public IList AuthenticationSchemes => ForbidResultSubject.AuthenticationSchemes; #endregion diff --git a/src/FluentAssertions.AspNetCore.Mvc/NotFoundObjectResultAssertions.cs b/src/FluentAssertions.AspNetCore.Mvc/NotFoundObjectResultAssertions.cs index c70d8b6..372f084 100644 --- a/src/FluentAssertions.AspNetCore.Mvc/NotFoundObjectResultAssertions.cs +++ b/src/FluentAssertions.AspNetCore.Mvc/NotFoundObjectResultAssertions.cs @@ -1,7 +1,5 @@ -using System.Diagnostics; -using FluentAssertions.Execution; -using FluentAssertions.Primitives; -using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc; +using System.Diagnostics; namespace FluentAssertions.AspNetCore.Mvc { @@ -9,7 +7,7 @@ namespace FluentAssertions.AspNetCore.Mvc /// Contains a number of methods to assert that a is in the expected state. /// [DebuggerNonUserCode] - public class NotFoundObjectResultAssertions : ObjectAssertions + public class NotFoundObjectResultAssertions : ObjectResultAssertionsBase { #region Public Constructors @@ -22,42 +20,5 @@ public NotFoundObjectResultAssertions(NotFoundObjectResult subject) : base(subje } #endregion - - #region Public Properties - /// - /// The value on the NotFoundObjectResult - /// - public object Value => NotFoundObjectResultSubject.Value; - - #endregion - - #region Private Properties - private NotFoundObjectResult NotFoundObjectResultSubject => (NotFoundObjectResult)Subject; - - #endregion - - #region Public Methods - /// - /// Asserts the value is of the expected type. - /// - /// The expected type. - /// The typed value. - public TValue ValueAs() - { - var value = NotFoundObjectResultSubject.Value; - - if (value == null) - Execute.Assertion - .WithDefaultIdentifier("NotFoundObjectResult.Value") - .FailWith(FailureMessages.CommonNullWasSuppliedFailMessage, typeof(TValue)); - - Execute.Assertion - .ForCondition(value is TValue) - .WithDefaultIdentifier("NotFoundObjectResult.Value") - .FailWith(FailureMessages.CommonTypeFailMessage, typeof(TValue), value.GetType()); - - return (TValue)value; - } - #endregion } } diff --git a/src/FluentAssertions.AspNetCore.Mvc/ObjectResultAssertions.cs b/src/FluentAssertions.AspNetCore.Mvc/ObjectResultAssertions.cs new file mode 100644 index 0000000..e6d4034 --- /dev/null +++ b/src/FluentAssertions.AspNetCore.Mvc/ObjectResultAssertions.cs @@ -0,0 +1,24 @@ +using Microsoft.AspNetCore.Mvc; +using System.Diagnostics; + +namespace FluentAssertions.AspNetCore.Mvc +{ + /// + /// Contains a number of methods to assert that a is in the expected state. + /// + [DebuggerNonUserCode] + public class ObjectResultAssertions : ObjectResultAssertionsBase + { + #region Public Constructors + /// + /// Initializes a new instance of the class. + /// + /// The object to test assertion on + public ObjectResultAssertions(ObjectResult subject) + : base(subject) + { + + } + #endregion + } +} diff --git a/src/FluentAssertions.AspNetCore.Mvc/ObjectResultAssertionsBase.cs b/src/FluentAssertions.AspNetCore.Mvc/ObjectResultAssertionsBase.cs new file mode 100644 index 0000000..25a76a4 --- /dev/null +++ b/src/FluentAssertions.AspNetCore.Mvc/ObjectResultAssertionsBase.cs @@ -0,0 +1,195 @@ +using FluentAssertions.Execution; +using FluentAssertions.Primitives; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.Formatters; +using System; +using System.Diagnostics; +using System.Linq; +using System.Linq.Expressions; + +namespace FluentAssertions.AspNetCore.Mvc +{ + /// + /// Base class for . + /// + [DebuggerNonUserCode] + public class ObjectResultAssertionsBase : ObjectAssertions + where TObjectResult : ObjectResult + where TObjectResultAssertion : ObjectResultAssertionsBase + { + #region Public Constructors + /// + /// Initializes a new instance of the class. + /// + /// The object to test assertion on + public ObjectResultAssertionsBase(TObjectResult subject) + : base(subject) + { + + } + #endregion + + #region Public Properties + + /// + /// The value on the ObjectResult + /// + public object Value => ObjectResultSubject.Value; + + #endregion + + #region Protected Properties + + /// + protected override string Identifier => typeof(TObjectResult).Name; + /// + /// The casted to the correct type. + /// + protected TObjectResult ObjectResultSubject => (TObjectResult)Subject; + + #endregion + + #region Public Methods + /// + /// Asserts the value is of the expected type. + /// + /// The expected type. + /// The typed value. + public TValue ValueAs() + { + var value = ObjectResultSubject.Value; + + if (value == null) + Execute.Assertion + .WithDefaultIdentifier($"{Identifier}.Value") + .FailWith(FailureMessages.CommonNullWasSuppliedFailMessage, typeof(TValue)); + + Execute.Assertion + .ForCondition(value is TValue) + .WithDefaultIdentifier($"{Identifier}.Value") + .FailWith(FailureMessages.CommonTypeFailMessage, typeof(TValue), value.GetType()); + + return (TValue)value; + } + + /// + /// Asserts that the contains at least one item that matches the predicate. + /// + /// A predicate to match the items in the against. + /// + /// A formatted phrase as is supported by explaining why the assertion + /// is needed. If the phrase does not start with the word because, it is prepended automatically. + /// + /// + /// Zero or more objects to format using the placeholders in . + /// + public TObjectResultAssertion ContainsFormatter(Expression> expectation, string reason = "", params object[] reasonArgs) + { + if (expectation is null) + throw new ArgumentNullException(nameof(expectation)); + + var formatters = ObjectResultSubject.Formatters; + + if (formatters is null) + { + Execute.Assertion + .BecauseOf(reason, reasonArgs) + .WithDefaultIdentifier(Identifier + ".Formatters") + .FailWith("Expected {context} to contain {0}{reason} but found {1}.", expectation.Body, formatters); + } + + var func = expectation.Compile(); + + Execute.Assertion + .ForCondition(formatters.Any(func)) + .WithDefaultIdentifier(Identifier+ ".Formatters") + .BecauseOf(reason, reasonArgs) + .FailWith("Expected {context} {0} to have an item matching {1}{reason}.", formatters, expectation.Body); + + return (TObjectResultAssertion)this; + } + + /// + /// Asserts that the contains the specified content type. + /// + /// The expectation content type. + /// + /// A formatted phrase as is supported by explaining why the assertion + /// is needed. If the phrase does not start with the word because, it is prepended automatically. + /// + /// + /// Zero or more objects to format using the placeholders in . + /// + public TObjectResultAssertion WithContentType(string expected, string reason = "", params object[] reasonArgs) + { + var contentTypes = ObjectResultSubject.ContentTypes; + + if (contentTypes is null) + { + Execute.Assertion + .BecauseOf(reason, reasonArgs) + .WithDefaultIdentifier(Identifier + ".ContentTypes") + .FailWith("Expected {context} to contain {0}{reason} but found {1}.", expected, contentTypes); + } + + Execute.Assertion + .ForCondition(contentTypes.Contains(expected)) + .WithDefaultIdentifier(Identifier + ".ContentTypes") + .BecauseOf(reason, reasonArgs) + .FailWith("Expected {context} {0} to contain {1}{reason}.", contentTypes, expected); + + return (TObjectResultAssertion)this; + } + + /// + /// Asserts that the is the expected declared type. + /// + /// The expected declared type. + /// + /// A formatted phrase as is supported by explaining why the assertion + /// is needed. If the phrase does not start with the word because, it is prepended automatically. + /// + /// + /// Zero or more objects to format using the placeholders in . + /// + public TObjectResultAssertion WithDeclaredType(Type expectedDeclaredType, string reason = "", params object[] reasonArgs) + { + var actual = ObjectResultSubject.DeclaredType; + + Execute.Assertion + .ForCondition(expectedDeclaredType == actual) + .WithDefaultIdentifier(Identifier + ".DeclaredType") + .BecauseOf(reason, reasonArgs) + .FailWith(FailureMessages.CommonTypeFailMessage, expectedDeclaredType, actual); + + return (TObjectResultAssertion)this; + } + + + /// + /// Asserts that the is the expected status code. + /// + /// The expected status code. + /// + /// A formatted phrase as is supported by explaining why the assertion + /// is needed. If the phrase does not start with the word because, it is prepended automatically. + /// + /// + /// Zero or more objects to format using the placeholders in . + /// + public TObjectResultAssertion WithStatusCode(int? expectedStatusCode, string reason = "", params object[] reasonArgs) + { + var actual = ObjectResultSubject.StatusCode; + + Execute.Assertion + .ForCondition(expectedStatusCode == actual) + .WithDefaultIdentifier(Identifier + ".StatusCode") + .BecauseOf(reason, reasonArgs) + .FailWith(FailureMessages.CommonFailMessage, expectedStatusCode, actual); + + return (TObjectResultAssertion)this; + } + #endregion + + } +} diff --git a/src/FluentAssertions.AspNetCore.Mvc/OkObjectResultAssertions.cs b/src/FluentAssertions.AspNetCore.Mvc/OkObjectResultAssertions.cs index e19de8c..5568775 100644 --- a/src/FluentAssertions.AspNetCore.Mvc/OkObjectResultAssertions.cs +++ b/src/FluentAssertions.AspNetCore.Mvc/OkObjectResultAssertions.cs @@ -1,7 +1,5 @@ -using System.Diagnostics; -using FluentAssertions.Execution; -using FluentAssertions.Primitives; -using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc; +using System.Diagnostics; namespace FluentAssertions.AspNetCore.Mvc { @@ -9,7 +7,7 @@ namespace FluentAssertions.AspNetCore.Mvc /// Contains a number of methods to assert that a is in the expected state. /// [DebuggerNonUserCode] - public class OkObjectResultAssertions : ObjectAssertions + public class OkObjectResultAssertions : ObjectResultAssertionsBase { #region Public Constructors @@ -22,42 +20,5 @@ public OkObjectResultAssertions(OkObjectResult subject) : base(subject) } #endregion - - #region Public Properties - /// - /// The value on the OkObjectResult - /// - public object Value => OkObjectResultSubject.Value; - - #endregion - - #region Private Properties - private OkObjectResult OkObjectResultSubject => (OkObjectResult)Subject; - - #endregion - - #region Public Methods - /// - /// Asserts the value is of the expected type. - /// - /// The expected type. - /// The typed value. - public TValue ValueAs() - { - var value = OkObjectResultSubject.Value; - - if (value == null) - Execute.Assertion - .WithDefaultIdentifier("OkObjectResult.Value") - .FailWith(FailureMessages.CommonNullWasSuppliedFailMessage, typeof(TValue)); - - Execute.Assertion - .ForCondition(value is TValue) - .WithDefaultIdentifier("OkObjectResult.Value") - .FailWith(FailureMessages.CommonTypeFailMessage, typeof(TValue), value.GetType()); - - return (TValue)value; - } - #endregion } } diff --git a/src/FluentAssertions.AspNetCore.Mvc/SignInResultAssertions.cs b/src/FluentAssertions.AspNetCore.Mvc/SignInResultAssertions.cs index 26e233a..4784765 100644 --- a/src/FluentAssertions.AspNetCore.Mvc/SignInResultAssertions.cs +++ b/src/FluentAssertions.AspNetCore.Mvc/SignInResultAssertions.cs @@ -28,14 +28,49 @@ public SignInResultAssertions(object subject) : base(subject) #endregion #region Public Properties + /// + /// The authentication properties on the SignInResult. + /// public AuthenticationProperties AuthenticationProperties => SignInResultSubject.Properties; + + /// + /// The items value in the authentication properties on the SignInResult. + /// public IDictionary Items => SignInResultSubject.Properties?.Items; + + /// + /// The is persistent value in the authentication properties on the SignInResult. + /// public bool IsPersistent => SignInResultSubject.Properties?.IsPersistent ?? false; + + /// + /// The redirect uri value in the authentication properties on the SignInResult. + /// public string RedirectUri => SignInResultSubject.Properties?.RedirectUri; + + /// + /// The issued utc value in the authentication properties on the SignInResult. + /// public DateTimeOffset? IssuedUtc => SignInResultSubject.Properties?.IssuedUtc; + + /// + /// The expires utc value in the authentication properties on the SignInResult. + /// public DateTimeOffset? ExpiresUtc => SignInResultSubject.Properties?.ExpiresUtc; + + /// + /// The allow refresh value in the authentication properties on the SignInResult. + /// public bool? AllowRefresh => SignInResultSubject.Properties?.AllowRefresh; + + /// + /// The authentication scheme value on the SignInResult. + /// public string AuthenticationScheme => SignInResultSubject.AuthenticationScheme; + + /// + /// The claims principal value on the SignInResult. + /// public ClaimsPrincipal Principal => SignInResultSubject.Principal; #endregion diff --git a/src/FluentAssertions.AspNetCore.Mvc/SignOutResultAssertions.cs b/src/FluentAssertions.AspNetCore.Mvc/SignOutResultAssertions.cs index dbc6ca0..14a45f1 100644 --- a/src/FluentAssertions.AspNetCore.Mvc/SignOutResultAssertions.cs +++ b/src/FluentAssertions.AspNetCore.Mvc/SignOutResultAssertions.cs @@ -28,13 +28,44 @@ public SignOutResultAssertions(object subject) : base(subject) #endregion #region Public Properties + /// + /// The authentication properties on the SignOutResult. + /// public AuthenticationProperties AuthenticationProperties => SignOutResultSubject.Properties; + + /// + /// The items value in the authentication properties on the SignOutResult. + /// public IDictionary Items => SignOutResultSubject.Properties?.Items; + + /// + /// The is persistent value in the authentication properties on the SignOutResult. + /// public bool IsPersistent => SignOutResultSubject.Properties?.IsPersistent ?? false; + + /// + /// The redirect uri value in the authentication properties on the SignOutResult. + /// public string RedirectUri => SignOutResultSubject.Properties?.RedirectUri; + + /// + /// The issued utc value in the authentication properties on the SignOutResult. + /// public DateTimeOffset? IssuedUtc => SignOutResultSubject.Properties?.IssuedUtc; + + /// + /// The expires utc value in the authentication properties on the SignOutResult. + /// public DateTimeOffset? ExpiresUtc => SignOutResultSubject.Properties?.ExpiresUtc; + + /// + /// The allow refresh value in the authentication properties on the SignOutResult. + /// public bool? AllowRefresh => SignOutResultSubject.Properties?.AllowRefresh; + + /// + /// The authentication schemes value on the SignOutResult. + /// public IList AuthenticationSchemes => SignOutResultSubject.AuthenticationSchemes; #endregion diff --git a/tests/FluentAssertions.AspNetCore.Mvc.Tests/AcceptedAtActionResultAssertions_Tests.cs b/tests/FluentAssertions.AspNetCore.Mvc.Tests/AcceptedAtActionResultAssertions_Tests.cs index 8778fb9..9ed392f 100644 --- a/tests/FluentAssertions.AspNetCore.Mvc.Tests/AcceptedAtActionResultAssertions_Tests.cs +++ b/tests/FluentAssertions.AspNetCore.Mvc.Tests/AcceptedAtActionResultAssertions_Tests.cs @@ -1,6 +1,6 @@ -using System; -using FluentAssertions.Mvc.Tests.Helpers; +using FluentAssertions.Mvc.Tests.Helpers; using Microsoft.AspNetCore.Mvc; +using System; using Xunit; namespace FluentAssertions.AspNetCore.Mvc.Tests diff --git a/tests/FluentAssertions.AspNetCore.Mvc.Tests/AcceptedAtRouteResultAssertions_Tests.cs b/tests/FluentAssertions.AspNetCore.Mvc.Tests/AcceptedAtRouteResultAssertions_Tests.cs index 34d520f..137e067 100644 --- a/tests/FluentAssertions.AspNetCore.Mvc.Tests/AcceptedAtRouteResultAssertions_Tests.cs +++ b/tests/FluentAssertions.AspNetCore.Mvc.Tests/AcceptedAtRouteResultAssertions_Tests.cs @@ -1,11 +1,11 @@ -using System; -using FluentAssertions.Mvc.Tests.Helpers; +using FluentAssertions.Mvc.Tests.Helpers; using Microsoft.AspNetCore.Mvc; +using System; using Xunit; namespace FluentAssertions.AspNetCore.Mvc.Tests { - + public class AcceptedAtRouteResultAssertions_Tests { public const string Reason = FailureMessageHelper.Reason; diff --git a/tests/FluentAssertions.AspNetCore.Mvc.Tests/AcceptedResultAssertions_Tests.cs b/tests/FluentAssertions.AspNetCore.Mvc.Tests/AcceptedResultAssertions_Tests.cs index ebaf4c3..22d22b6 100644 --- a/tests/FluentAssertions.AspNetCore.Mvc.Tests/AcceptedResultAssertions_Tests.cs +++ b/tests/FluentAssertions.AspNetCore.Mvc.Tests/AcceptedResultAssertions_Tests.cs @@ -1,6 +1,6 @@ -using System; -using FluentAssertions.Mvc.Tests.Helpers; +using FluentAssertions.Mvc.Tests.Helpers; using Microsoft.AspNetCore.Mvc; +using System; using Xunit; namespace FluentAssertions.AspNetCore.Mvc.Tests @@ -38,7 +38,7 @@ public void ValueAs_GivenWrongType_ShouldFail() { var result = new TestController().Accepted(TestUri, TestValue); var failureMessage = FailureMessageHelper.ExpectedContextTypeXButFoundY( - "AcceptedResultAssertions.Value", typeof(int), typeof(string)); + "AcceptedResult.Value", typeof(int), typeof(string)); Action a = () => result.Should().BeAcceptedResult().ValueAs().Should().Be(2); @@ -50,7 +50,7 @@ public void ValueAs_Null_ShouldFail() { ActionResult result = new AcceptedResult(TestUri, null); var failureMessage = FailureMessageHelper.ExpectedContextTypeXButFoundNull( - "AcceptedResultAssertions.Value", typeof(object)); + "AcceptedResult.Value", typeof(object)); Action a = () => result.Should().BeAcceptedResult().ValueAs(); @@ -69,7 +69,7 @@ public void WithUri_GivenExpectedUri_ShouldPass() public void WithUri_GivenWrongUri_ShouldFail() { var result = new TestController().Accepted(TestWrongUri, TestValue); - var failureMessage = FailureMessageHelper.ExpectedContextToBeXButY("AcceptedResultAssertions.Uri", TestUri.ToString(), TestWrongUri.ToString()); + var failureMessage = FailureMessageHelper.ExpectedContextToBeXButY("AcceptedResult.Uri", TestUri.ToString(), TestWrongUri.ToString()); Action a = () => result.Should().BeAcceptedResult().WithUri(TestUri, Reason, ReasonArgs); @@ -88,7 +88,7 @@ public void WithUri_GivenExpectedUriAsString_ShouldPass() public void WithUri_GivenWrongUriAsString_ShouldFail() { var result = new TestController().Accepted(TestWrongUriAsString, TestValue); - var failureMessage = FailureMessageHelper.ExpectedContextToBeXButY("AcceptedResultAssertions.Uri", TestUriAsString, TestWrongUriAsString); + var failureMessage = FailureMessageHelper.ExpectedContextToBeXButY("AcceptedResult.Uri", TestUriAsString, TestWrongUriAsString); Action a = () => result.Should().BeAcceptedResult().WithUri(TestUriAsString, Reason, ReasonArgs); diff --git a/tests/FluentAssertions.AspNetCore.Mvc.Tests/ActionResultAssertions_Tests.cs b/tests/FluentAssertions.AspNetCore.Mvc.Tests/ActionResultAssertions_Tests.cs index 2c0a9ee..c68c214 100644 --- a/tests/FluentAssertions.AspNetCore.Mvc.Tests/ActionResultAssertions_Tests.cs +++ b/tests/FluentAssertions.AspNetCore.Mvc.Tests/ActionResultAssertions_Tests.cs @@ -27,7 +27,7 @@ public void BeContent_GivenContent_ShouldPass() public void BeContent_GivenNotContent_ShouldFail() { ActionResult result = new ViewResult(); - var failureMessage = FailureMessageHelper.ExpectedContextTypeXButFoundY("result", typeof(ContentResult), typeof(ViewResult)); + var failureMessage = FailureMessageHelper.ExpectedContextTypeXButFoundYWithReason("result", typeof(ContentResult), typeof(ViewResult)); Action a = () => result.Should().BeContentResult(Reason, ReasonArgs); @@ -47,7 +47,7 @@ public void BeEmpty_GivenEmpty_ShouldPass() public void BeEmpty_GivenNotEmpty_ShouldPass() { ActionResult result = new ViewResult(); - var failureMessage = FailureMessageHelper.ExpectedContextTypeXButFoundY("result", typeof(EmptyResult), typeof(ViewResult)); + var failureMessage = FailureMessageHelper.ExpectedContextTypeXButFoundYWithReason("result", typeof(EmptyResult), typeof(ViewResult)); Action a = () => result.Should().BeEmptyResult(Reason, ReasonArgs); @@ -68,7 +68,7 @@ public void BeFileResult_GivenFileResult_ShouldPass() public void BeFileResult_GivenNotFileResult_ShouldFail() { ActionResult result = new ViewResult(); - var failureMessage = FailureMessageHelper.ExpectedContextTypeXButFoundY("result", typeof(FileResult), typeof(ViewResult)); + var failureMessage = FailureMessageHelper.ExpectedContextTypeXButFoundYWithReason("result", typeof(FileResult), typeof(ViewResult)); Action a = () => result.Should().BeFileResult(Reason, ReasonArgs); @@ -89,7 +89,7 @@ public void BeFileContentResult_GivenFileContentResult_ShouldPass() public void BeFileContentResult_GivenNotFileContentResult_ShouldFail() { ActionResult result = new ViewResult(); - var failureMessage = FailureMessageHelper.ExpectedContextTypeXButFoundY("result", typeof(FileContentResult), typeof(ViewResult)); + var failureMessage = FailureMessageHelper.ExpectedContextTypeXButFoundYWithReason("result", typeof(FileContentResult), typeof(ViewResult)); Action a = () => result.Should().BeFileContentResult(Reason, ReasonArgs); @@ -110,7 +110,7 @@ public void BeFileStreamResult_GivenFileStreamResult_ShouldPass() public void BeFileStreamResult_GivenNotFileStreamResult_ShouldFail() { ActionResult result = new ViewResult(); - var failureMessage = FailureMessageHelper.ExpectedContextTypeXButFoundY("result", typeof(FileStreamResult), typeof(ViewResult)); + var failureMessage = FailureMessageHelper.ExpectedContextTypeXButFoundYWithReason("result", typeof(FileStreamResult), typeof(ViewResult)); Action a = () => result.Should().BeFileStreamResult(Reason, ReasonArgs); @@ -131,7 +131,7 @@ public void BePhysicalFileResult_GivenPhysicalFileResult_ShouldPass() public void BePhysicalFileResult_GivenNotPhysicalFileResult_ShouldFail() { ActionResult result = new ViewResult(); - var failureMessage = FailureMessageHelper.ExpectedContextTypeXButFoundY("result", typeof(PhysicalFileResult), typeof(ViewResult)); + var failureMessage = FailureMessageHelper.ExpectedContextTypeXButFoundYWithReason("result", typeof(PhysicalFileResult), typeof(ViewResult)); Action a = () => result.Should().BePhysicalFileResult(Reason, ReasonArgs); @@ -152,7 +152,7 @@ public void BeVirtualFileResult_GivenVirtualFileResult_ShouldPass() public void BeVirtualFileResult_GivenNotVirtualFileResult_ShouldFail() { ActionResult result = new ViewResult(); - var failureMessage = FailureMessageHelper.ExpectedContextTypeXButFoundY("result", typeof(VirtualFileResult), typeof(ViewResult)); + var failureMessage = FailureMessageHelper.ExpectedContextTypeXButFoundYWithReason("result", typeof(VirtualFileResult), typeof(ViewResult)); Action a = () => result.Should().BeVirtualFileResult(Reason, ReasonArgs); @@ -174,7 +174,7 @@ public void BeJson_GivenJson_ShouldPass() public void BeJson_GivenNotJson_ShouldFail() { ActionResult result = new ViewResult(); - var failureMessage = FailureMessageHelper.ExpectedContextTypeXButFoundY("result", typeof(JsonResult), typeof(ViewResult)); + var failureMessage = FailureMessageHelper.ExpectedContextTypeXButFoundYWithReason("result", typeof(JsonResult), typeof(ViewResult)); Action a = () => result.Should().BeJsonResult(Reason, ReasonArgs); @@ -194,7 +194,7 @@ public void BeRedirectToRoute_GivenRedirectToRoute_ShouldPass() public void BeRedirectToRoute_GivenNotRedirectToRoute_ShouldFail() { ActionResult result = new ViewResult(); - var failureMessage = FailureMessageHelper.ExpectedContextTypeXButFoundY("result", typeof(RedirectToRouteResult), typeof(ViewResult)); + var failureMessage = FailureMessageHelper.ExpectedContextTypeXButFoundYWithReason("result", typeof(RedirectToRouteResult), typeof(ViewResult)); Action a = () => result.Should().BeRedirectToRouteResult(Reason, ReasonArgs); @@ -214,7 +214,7 @@ public void BeRedirect_GivenRedirect_ShouldPass() public void BeRedirect_GivenNotRedirect_ShouldFail() { ActionResult result = new ViewResult(); - var failureMessage = FailureMessageHelper.ExpectedContextTypeXButFoundY("result", typeof(RedirectResult), typeof(ViewResult)); + var failureMessage = FailureMessageHelper.ExpectedContextTypeXButFoundYWithReason("result", typeof(RedirectResult), typeof(ViewResult)); Action a = () => result.Should().BeRedirectResult(Reason, ReasonArgs); @@ -234,7 +234,7 @@ public void BePartialView_GivenPartial_ShouldPass() public void BePartialView_GivenNotPartial_ShouldFail() { ActionResult result = new ViewResult(); - var failureMessage = FailureMessageHelper.ExpectedContextTypeXButFoundY("result", typeof(PartialViewResult), typeof(ViewResult)); + var failureMessage = FailureMessageHelper.ExpectedContextTypeXButFoundYWithReason("result", typeof(PartialViewResult), typeof(ViewResult)); Action a = () => result.Should().BePartialViewResult(Reason, ReasonArgs); @@ -254,7 +254,7 @@ public void BeView_GivenView_ShouldPass() public void BeView_GivenNotView_ShouldFail() { ActionResult result = new RedirectResult("/"); - var failureMessage = FailureMessageHelper.ExpectedContextTypeXButFoundY("result", typeof(ViewResult), typeof(RedirectResult)); + var failureMessage = FailureMessageHelper.ExpectedContextTypeXButFoundYWithReason("result", typeof(ViewResult), typeof(RedirectResult)); Action a = () => result.Should().BeViewResult(Reason, ReasonArgs); @@ -274,7 +274,7 @@ public void BeStatusCodeResult_GivenStatusCodeResult_ShouldPass() public void BeStatusCodeResult_GivenNotStatusCodeResult_ShouldFail() { ActionResult result = new ViewResult(); - var failureMessage = FailureMessageHelper.ExpectedContextTypeXButFoundY("result", typeof(StatusCodeResult), typeof(ViewResult)); + var failureMessage = FailureMessageHelper.ExpectedContextTypeXButFoundYWithReason("result", typeof(StatusCodeResult), typeof(ViewResult)); Action a = () => result.Should().BeStatusCodeResult(Reason, ReasonArgs); @@ -282,6 +282,26 @@ public void BeStatusCodeResult_GivenNotStatusCodeResult_ShouldFail() .WithMessage(failureMessage); } + [Fact] + public void BeObjectResult_GivenObjectResult_ShouldPass() + { + ActionResult result = new ObjectResult("TestValue"); + + result.Should().BeObjectResult(); + } + + [Fact] + public void BeObjectResult_GivenNotObjectResult_ShouldFail() + { + ActionResult result = new ViewResult(); + var failureMessage = FailureMessageHelper.ExpectedContextTypeXButFoundYWithReason("result", typeof(ObjectResult), typeof(ViewResult)); + + Action a = () => result.Should().BeObjectResult(Reason, ReasonArgs); + + a.Should().Throw() + .WithMessage(failureMessage); + } + [Fact] public void BeOkResult_GivenOk_ShouldPass() { @@ -294,7 +314,7 @@ public void BeOkResult_GivenOk_ShouldPass() public void BeOkResult_GivenNotOk_ShouldFail() { ActionResult result = new ViewResult(); - var failureMessage = FailureMessageHelper.ExpectedContextTypeXButFoundY("result", typeof(OkResult), typeof(ViewResult)); + var failureMessage = FailureMessageHelper.ExpectedContextTypeXButFoundYWithReason("result", typeof(OkResult), typeof(ViewResult)); Action a = () => result.Should().BeOkResult(Reason, ReasonArgs); @@ -314,7 +334,7 @@ public void BeBadRequestResult_GivenBadRequest_ShouldPass() public void BeBadRequestResult_GivenNotBadRequest_ShouldFail() { ActionResult result = new ViewResult(); - var failureMessage = FailureMessageHelper.ExpectedContextTypeXButFoundY("result", typeof(BadRequestResult), typeof(ViewResult)); + var failureMessage = FailureMessageHelper.ExpectedContextTypeXButFoundYWithReason("result", typeof(BadRequestResult), typeof(ViewResult)); Action a = () => result.Should().BeBadRequestResult(Reason, ReasonArgs); @@ -334,7 +354,7 @@ public void BeChallengeResult_GivenChallengeResult_ShouldPass() public void BeChallengeResult_GivenNotChallengeResult_ShouldFail() { ActionResult result = new ViewResult(); - var failureMessage = FailureMessageHelper.ExpectedContextTypeXButFoundY("result", typeof(ChallengeResult), typeof(ViewResult)); + var failureMessage = FailureMessageHelper.ExpectedContextTypeXButFoundYWithReason("result", typeof(ChallengeResult), typeof(ViewResult)); Action a = () => result.Should().BeChallengeResult(Reason, ReasonArgs); @@ -353,7 +373,7 @@ public void BeAcceptedResult_GivenAccepted_ShouldPass() public void BeAcceptedResult_GivenNotAccepted_ShouldFail() { ActionResult result = new ViewResult(); - var failureMessage = FailureMessageHelper.ExpectedContextTypeXButFoundY("result", typeof(AcceptedResult), typeof(ViewResult)); + var failureMessage = FailureMessageHelper.ExpectedContextTypeXButFoundYWithReason("result", typeof(AcceptedResult), typeof(ViewResult)); Action a = () => result.Should().BeAcceptedResult(Reason, ReasonArgs); @@ -373,7 +393,7 @@ public void BeNoContentResult_GivenNoContent_ShouldPass() public void BeNoContentResult_GivenNotNoContent_ShouldFail() { ActionResult result = new ViewResult(); - var failureMessage = FailureMessageHelper.ExpectedContextTypeXButFoundY("result", typeof(NoContentResult), typeof(ViewResult)); + var failureMessage = FailureMessageHelper.ExpectedContextTypeXButFoundYWithReason("result", typeof(NoContentResult), typeof(ViewResult)); Action a = () => result.Should().BeNoContentResult(Reason, ReasonArgs); @@ -393,7 +413,7 @@ public void BeNotFoundResult_GivenNotFound_ShouldPass() public void BeNotFoundResult_GivenNotNotFound_ShouldFail() { ActionResult result = new ViewResult(); - var failureMessage = FailureMessageHelper.ExpectedContextTypeXButFoundY("result", typeof(NotFoundResult), typeof(ViewResult)); + var failureMessage = FailureMessageHelper.ExpectedContextTypeXButFoundYWithReason("result", typeof(NotFoundResult), typeof(ViewResult)); Action a = () => result.Should().BeNotFoundResult(Reason, ReasonArgs); @@ -413,7 +433,7 @@ public void BeUnauthorizedResult_GivenUnauthorized_ShouldPass() public void BeUnauthorizedResult_GivenNotUnauthorized_ShouldFail() { ActionResult result = new ViewResult(); - var failureMessage = FailureMessageHelper.ExpectedContextTypeXButFoundY("result", typeof(UnauthorizedResult), typeof(ViewResult)); + var failureMessage = FailureMessageHelper.ExpectedContextTypeXButFoundYWithReason("result", typeof(UnauthorizedResult), typeof(ViewResult)); Action a = () => result.Should().BeUnauthorizedResult(Reason, ReasonArgs); @@ -433,7 +453,7 @@ public void BeSignOutResult_GivenSignOutResult_ShouldPass() public void BeSignOutResult_GivenNotSignOutResult_ShouldFail() { ActionResult result = new ViewResult(); - var failureMessage = FailureMessageHelper.ExpectedContextTypeXButFoundY("result", typeof(SignOutResult), typeof(ViewResult)); + var failureMessage = FailureMessageHelper.ExpectedContextTypeXButFoundYWithReason("result", typeof(SignOutResult), typeof(ViewResult)); Action a = () => result.Should().BeSignOutResult(Reason, ReasonArgs); @@ -454,7 +474,7 @@ public void BeLocalRedirectResult_GivenLocalRedirectResult_ShouldPass() public void BeLocalRedirectResult_GivenNotLocalRedirectResult_ShouldFail() { ActionResult result = new ViewResult(); - var failureMessage = FailureMessageHelper.ExpectedContextTypeXButFoundY("result", typeof(LocalRedirectResult), typeof(ViewResult)); + var failureMessage = FailureMessageHelper.ExpectedContextTypeXButFoundYWithReason("result", typeof(LocalRedirectResult), typeof(ViewResult)); Action a = () => result.Should().BeLocalRedirectResult(Reason, ReasonArgs); diff --git a/tests/FluentAssertions.AspNetCore.Mvc.Tests/ConvertToActionResultAssertions_Tests.cs b/tests/FluentAssertions.AspNetCore.Mvc.Tests/ConvertToActionResultAssertions_Tests.cs index 2b4ffb5..2f92481 100644 --- a/tests/FluentAssertions.AspNetCore.Mvc.Tests/ConvertToActionResultAssertions_Tests.cs +++ b/tests/FluentAssertions.AspNetCore.Mvc.Tests/ConvertToActionResultAssertions_Tests.cs @@ -62,8 +62,8 @@ public void BeActionResult_GivenUnexpectedType_ShouldFail() { var mock = new Mock(); var result = mock.Object; - var failureMessage = FailureMessageHelper.ExpectedContextTypeXButFoundY( - "result", "Microsoft.AspNetCore.Mvc.ActionResult`1[System.Object]", result.GetType().FullName); + var failureMessage = FailureMessageHelper.ExpectedContextTypeXButFoundYWithReason( + "result", typeof(ActionResult), result.GetType()); Action action = () => result.Should().BeActionResult(Reason, ReasonArgs); diff --git a/tests/FluentAssertions.AspNetCore.Mvc.Tests/CreatedAtActionResultAssertions_Tests.cs b/tests/FluentAssertions.AspNetCore.Mvc.Tests/CreatedAtActionResultAssertions_Tests.cs index c8f0247..a65e0b0 100644 --- a/tests/FluentAssertions.AspNetCore.Mvc.Tests/CreatedAtActionResultAssertions_Tests.cs +++ b/tests/FluentAssertions.AspNetCore.Mvc.Tests/CreatedAtActionResultAssertions_Tests.cs @@ -1,11 +1,11 @@ -using System; -using FluentAssertions.Mvc.Tests.Helpers; +using FluentAssertions.Mvc.Tests.Helpers; using Microsoft.AspNetCore.Mvc; +using System; using Xunit; namespace FluentAssertions.AspNetCore.Mvc.Tests { - + public class CreatedAtActionResultAssertions_Tests { public const string Reason = FailureMessageHelper.Reason; diff --git a/tests/FluentAssertions.AspNetCore.Mvc.Tests/CreatedAtRouteResultAssertions_Tests.cs b/tests/FluentAssertions.AspNetCore.Mvc.Tests/CreatedAtRouteResultAssertions_Tests.cs index 01d0a26..cea97c9 100644 --- a/tests/FluentAssertions.AspNetCore.Mvc.Tests/CreatedAtRouteResultAssertions_Tests.cs +++ b/tests/FluentAssertions.AspNetCore.Mvc.Tests/CreatedAtRouteResultAssertions_Tests.cs @@ -1,6 +1,6 @@ -using System; -using FluentAssertions.Mvc.Tests.Helpers; +using FluentAssertions.Mvc.Tests.Helpers; using Microsoft.AspNetCore.Mvc; +using System; using Xunit; namespace FluentAssertions.AspNetCore.Mvc.Tests diff --git a/tests/FluentAssertions.AspNetCore.Mvc.Tests/CreatedResultAssertions_Tests.cs b/tests/FluentAssertions.AspNetCore.Mvc.Tests/CreatedResultAssertions_Tests.cs index 6bcb790..402633b 100644 --- a/tests/FluentAssertions.AspNetCore.Mvc.Tests/CreatedResultAssertions_Tests.cs +++ b/tests/FluentAssertions.AspNetCore.Mvc.Tests/CreatedResultAssertions_Tests.cs @@ -77,7 +77,7 @@ public void WithUri_GivenExpectedUri_ShouldPass() public void WithUri_GivenWrongUri_ShouldFail() { var result = new TestController().Created(TestWrongUri, TestValue); - var failureMessage = FailureMessageHelper.ExpectedContextToBeXButY("CreatedResult.Uri", TestUri, TestWrongUri); + var failureMessage = FailureMessageHelper.ExpectedContextToBeXButY("CreatedResult.Uri", TestUri.ToString(), TestWrongUri.ToString()); Action a = () => result.Should().BeCreatedResult().WithUri(TestUri, Reason, ReasonArgs); diff --git a/tests/FluentAssertions.AspNetCore.Mvc.Tests/FileResultAssertions_Tests.cs b/tests/FluentAssertions.AspNetCore.Mvc.Tests/FileResultAssertions_Tests.cs index 622ea19..7770741 100644 --- a/tests/FluentAssertions.AspNetCore.Mvc.Tests/FileResultAssertions_Tests.cs +++ b/tests/FluentAssertions.AspNetCore.Mvc.Tests/FileResultAssertions_Tests.cs @@ -16,7 +16,7 @@ public class FileResultAssertions_Tests public void WithContentType_GivenExpectedValue_ShouldPass() { var actualValue = "text/plain"; - var expectedValue = string.Copy(actualValue); + var expectedValue = actualValue; ActionResult result = TestDataGenerator.CreateFileContentResult(contentType: actualValue); result.Should().BeFileResult().WithContentType(expectedValue); diff --git a/tests/FluentAssertions.AspNetCore.Mvc.Tests/Helpers/FailureMessageHelper.cs b/tests/FluentAssertions.AspNetCore.Mvc.Tests/Helpers/FailureMessageHelper.cs index da483f7..2bc7868 100644 --- a/tests/FluentAssertions.AspNetCore.Mvc.Tests/Helpers/FailureMessageHelper.cs +++ b/tests/FluentAssertions.AspNetCore.Mvc.Tests/Helpers/FailureMessageHelper.cs @@ -1,4 +1,5 @@ -using System; +using FluentAssertions.Formatting; +using System; using System.Globalization; using System.Linq; using System.Text; @@ -17,67 +18,44 @@ public static string Format(string message, params string[] args) return String.Format(message, formattedArg); } - public static string ExpectedContextToBeXButY(string context, Uri expected, Uri actual) - { - return ExpectedContextToBeXButY(context, expected?.ToString(), actual?.ToString()); - } - - public static string ExpectedContextToBeXButY(string context, string expected, string actual) + public static string ExpectedContextToBeXButY(string context, object expected, object actual) { - return ExpectedContextToBeXButY(context, (object)$"\"{expected}\"", (object)$"\"{actual}\""); + return $"Expected {context} to be {Formatter.ToString(expected)} because it is 10 but found {Formatter.ToString(actual)}."; } - public static string ExpectedContextToBeXButY(string context, DateTimeOffset? expected, DateTimeOffset? actual) + public static string ExpectedAtKeyValueXButFoundY(string context, string key, string expected, string actual) { - return ExpectedContextToBeXButY(context, ToString(expected), ToString(actual)); + return $"Expected {context} to contain value \"{expected}\" at key \"{key}\" because it is 10 but found \"{actual}\"."; } - public static string ExpectedContextToBeXButY(string context, object expected, object actual) + public static string ExpectedKeyButNotFound(string context, string key, string expected) { - return $"Expected {context} to be {expected} because it is 10 but found {actual}."; + return $"Expected {context} to contain value \"{expected}\" at key \"{key}\" because it is 10, but the key was not found."; } - public static string ExpectedAtKeyValueXButFoundY(string context, string key, string expected, string actual) + internal static string ExpectedToContainItemButFoundNull(string context, object item) { - return $"Expected {context} to contain value \"{expected}\" at key \"{key}\" because it is 10 but found \"{actual}\"."; + return $"Expected {context} to contain {Formatter.ToString(item)} because it is 10 but found ."; } - public static string ExpectedKeyButNotFound(string context, string key, string expected) + internal static string ExpectedToHaveItemMatching(string context, object list, string predicate) { - return $"Expected {context} to contain value \"{expected}\" at key \"{key}\" because it is 10, but the key was not found."; + return $"Expected {context} {Formatter.ToString(list)} to have an item matching {predicate} because it is 10."; } - private static object ToString(DateTimeOffset? expected) + internal static string ExpectedToContainItem(string context, object list, string expected) { - if (expected.HasValue) - { - var builder = new StringBuilder($"<{expected:yyyy-MM-dd HH:mm:ss}"); - if (expected.Value.Millisecond > 0) - { - builder.AppendFormat(".{0:fffffff}", expected.Value); - } - if (expected.Value.Offset.Hours > 0) - { - builder.AppendFormat(" +{0}h", expected.Value.Offset.Hours); - } - else if (expected.Value.Offset.Hours < 0) - { - builder.AppendFormat(" -{0}h", expected.Value.Offset.Hours); - } - builder.Append('>'); - return builder.ToString(); - } - return ""; + return $"Expected {context} {Formatter.ToString(list)} to contain {Formatter.ToString(expected)} because it is 10."; } internal static string ExpectedContextTypeXButFoundY(string context, Type expected, Type actual) { - return ExpectedContextTypeXButFoundY(context, expected.FullName, actual.FullName); + return $"Expected {context} to be of type {Formatter.ToString(expected)} but was {Formatter.ToString(actual)}."; } - internal static string ExpectedContextTypeXButFoundY(string context, string expected, string actual) + internal static string ExpectedContextTypeXButFoundYWithReason(string context, Type expected, Type actual) { - return $"Expected {context} to be of type {expected} but was {actual}."; + return $"Expected {context} to be of type {Formatter.ToString(expected)} but was {Formatter.ToString(actual)} because it is 10."; } internal static string ExpectedContextTypeXButFoundNull(string context, Type expectedType) diff --git a/tests/FluentAssertions.AspNetCore.Mvc.Tests/ObjectResultAssertions_Tests.cs b/tests/FluentAssertions.AspNetCore.Mvc.Tests/ObjectResultAssertions_Tests.cs new file mode 100644 index 0000000..19df4e6 --- /dev/null +++ b/tests/FluentAssertions.AspNetCore.Mvc.Tests/ObjectResultAssertions_Tests.cs @@ -0,0 +1,223 @@ +using System; +using System.Linq.Expressions; +using System.Net.Http.Headers; +using FluentAssertions.Mvc.Tests.Helpers; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.Formatters; +using Moq; +using Xunit; + +namespace FluentAssertions.AspNetCore.Mvc.Tests +{ + public class ObjectResultAssertions_Tests + { + private const string TestValue = "testValue"; + public const string Reason = FailureMessageHelper.Reason; + public readonly static object[] ReasonArgs = FailureMessageHelper.ReasonArgs; + + [Fact] + public void Value_GivenObjectResult_ShouldHaveTheSameValue() + { + var result = new ObjectResult(TestValue); + result.Should().BeObjectResult().Value.Should().BeSameAs(TestValue); + } + + [Fact] + public void ValueAs_GivenObjectResult_ShouldHaveTheSameValue() + { + var result = new ObjectResult(TestValue); + + result.Should().BeObjectResult().ValueAs().Should().BeSameAs(TestValue); + } + + [Fact] + public void ValueAs_GivenWrongType_ShouldFail() + { + var result = new ObjectResult(TestValue); + string failureMessage = FailureMessageHelper.ExpectedContextTypeXButFoundY( + "ObjectResult.Value", typeof(int), typeof(string)); + + Action a = () => result.Should().BeObjectResult().ValueAs().Should().Be(2); + + a.Should().Throw() + .WithMessage(failureMessage); + } + + [Fact] + public void ValueAs_Null_ShouldFail() + { + ActionResult result = new ObjectResult(null); + string failureMessage = FailureMessageHelper.ExpectedContextTypeXButFoundNull( + "ObjectResult.Value", typeof(object)); + + Action a = () => result.Should().BeObjectResult().ValueAs(); + + a.Should().Throw() + .WithMessage(failureMessage); + } + + [Fact] + public void ContainsFormatter_GivenExpected_ShouldPass() + { + var formatterMock = new Mock(); + var result = new ObjectResult(TestValue) + { + Formatters = { formatterMock.Object } + }; + + result.Should().BeObjectResult().ContainsFormatter(formatter => ReferenceEquals(formatter, formatterMock.Object)); + } + + [Fact] + public void ContainsFormatter_OnNullFormatters_ShouldFail() + { + var result = new ObjectResult(TestValue) + { + Formatters = null + }; + Expression> expectation = formatter => formatter == null; + string failureMessage = FailureMessageHelper.ExpectedToContainItemButFoundNull( + "ObjectResult.Formatters", + expectation.Body); + + Action a = () => result.Should().BeObjectResult().ContainsFormatter(expectation, Reason, ReasonArgs); + + a.Should().Throw() + .WithMessage(failureMessage); + } + + [Fact] + public void ContainsFormatter_GivenUnexpected_ShouldFail() + { + var formatterMock = new Mock(); + var result = new ObjectResult(TestValue) + { + Formatters = { formatterMock.Object } + }; + string failureMessage = FailureMessageHelper.ExpectedToHaveItemMatching( + "ObjectResult.Formatters", + result.Formatters, + "False"); + + Action a = () => result.Should().BeObjectResult().ContainsFormatter(formatter => false, Reason, ReasonArgs); + + a.Should().Throw() + .WithMessage(failureMessage); + } + + [Fact] + public void WithContentType_GivenExpected_ShouldPass() + { + var mediaType = "text/html"; + var result = new ObjectResult(TestValue) + { + ContentTypes = { mediaType } + }; + + result.Should().BeObjectResult().WithContentType(mediaType); + } +#if NETCOREAPP2_1 + // In NetCore3.1 ContentTypes cant be null. + [Fact] + public void WithContentType_OnNullMediaType_ShouldFail() + { + var expected = "text/json"; + var result = new ObjectResult(TestValue) + { + ContentTypes = null + }; + string failureMessage = FailureMessageHelper.ExpectedToContainItemButFoundNull( + "ObjectResult.ContentTypes", + expected); + + Action a = () => result.Should().BeObjectResult().WithContentType(expected, Reason, ReasonArgs); + + a.Should().Throw() + .WithMessage(failureMessage); + } +#endif + [Fact] + public void WithContentType_GivenUnexpected_ShouldFail() + { + var mediaType = "text/html"; + var expected = "text/json"; + var result = new ObjectResult(TestValue) + { + ContentTypes = { mediaType } + }; + string failureMessage = FailureMessageHelper.ExpectedToContainItem( + "ObjectResult.ContentTypes", + result.ContentTypes, + expected); + + Action a = () => result.Should().BeObjectResult().WithContentType(expected, Reason, ReasonArgs); + + a.Should().Throw() + .WithMessage(failureMessage); + } + + [Fact] + public void WithDeclaredType_GivenExpected_ShouldPass() + { + var expectedType = typeof(string); + var result = new ObjectResult(TestValue) + { + DeclaredType = expectedType + }; + + result.Should().BeObjectResult().WithDeclaredType(expectedType); + } + + [Fact] + public void WithDeclaredType_GivenUnexpected_ShouldFail() + { + var expectedType = typeof(int); + var declaredType = typeof(string); + var result = new ObjectResult(TestValue) + { + DeclaredType = declaredType + }; + string failureMessage = FailureMessageHelper.ExpectedContextTypeXButFoundYWithReason( + "ObjectResult.DeclaredType", + expectedType, + declaredType); + + Action a = () => result.Should().BeObjectResult().WithDeclaredType(expectedType, Reason, ReasonArgs); + + a.Should().Throw() + .WithMessage(failureMessage); + } + + [Fact] + public void WithStatusCode_GivenExpected_ShouldPass() + { + var expectedStatusCode = 200; + var result = new ObjectResult(TestValue) + { + StatusCode = expectedStatusCode + }; + + result.Should().BeObjectResult().WithStatusCode(expectedStatusCode); + } + + [Fact] + public void WithStatusCode_GivenUnexpected_ShouldFail() + { + var expectedStatusCode = 200; + var actualStatusCode = 500; + var result = new ObjectResult(TestValue) + { + StatusCode = actualStatusCode + }; + string failureMessage = FailureMessageHelper.ExpectedContextToBeXButY( + "ObjectResult.StatusCode", + expectedStatusCode, + actualStatusCode); + + Action a = () => result.Should().BeObjectResult().WithStatusCode(expectedStatusCode, Reason, ReasonArgs); + + a.Should().Throw() + .WithMessage(failureMessage); + } + } +}