/// <summary> /// Tests whether the <see cref="Microsoft.AspNetCore.Mvc.ActionResult"/> /// location passes the given predicate. /// </summary> /// <param name="baseTestBuilderWithRedirectResult"> /// Instance of <see cref="IBaseTestBuilderWithRedirectResult{TRedirectResultTestBuilder}"/> type. /// </param> /// <param name="predicate">Predicate testing the location.</param> /// <returns>The same redirect <see cref="Microsoft.AspNetCore.Mvc.ActionResult"/> test builder.</returns> public static TRedirectResultTestBuilder ToUrlPassing <TRedirectResultTestBuilder>( this IBaseTestBuilderWithRedirectResult <TRedirectResultTestBuilder> baseTestBuilderWithRedirectResult, Func <string, bool> predicate) where TRedirectResultTestBuilder : IBaseTestBuilderWithActionResult { var actualBuilder = GetActualBuilder(baseTestBuilderWithRedirectResult); RuntimeBinderValidator.ValidateBinding(() => { var url = actualBuilder .TestContext .MethodResult .AsDynamic() .Url; if (!predicate(url)) { actualBuilder.ThrowNewFailedValidationException( $"location ('{url}')", "to pass the given predicate", "it failed"); } }); return(actualBuilder.ResultTestBuilder); }
internal static TViewResultTestBuilder WithDefaultName <TViewResultTestBuilder>( this IBaseTestBuilderWithViewResult <TViewResultTestBuilder> baseTestBuilderWithViewResult) where TViewResultTestBuilder : IBaseTestBuilderWithActionResult { var actualBuilder = GetActualBuilder(baseTestBuilderWithViewResult); RuntimeBinderValidator.ValidateBinding(() => { var actualViewName = actualBuilder .TestContext .MethodResult .AsDynamic() .ViewName; if (actualViewName != null) { actualBuilder.ThrowNewFailedValidationException( "name", $"to be {TestHelper.GetFriendlyName(null)}", $"instead received {TestHelper.GetFriendlyName(actualViewName)}"); } }); return(actualBuilder.ResultTestBuilder); }
/// <summary> /// Tests whether the redirect <see cref="Microsoft.AspNetCore.Mvc.ActionResult"/> is preserving the request method. /// </summary> /// <param name="baseTestBuilderWithRedirectResult"> /// Instance of <see cref="IBaseTestBuilderWithRedirectResult{TRedirectResultTestBuilder}"/> type. /// </param> /// <param name="preserveMethod">Expected boolean value.</param> /// <returns>The same redirect <see cref="Microsoft.AspNetCore.Mvc.ActionResult"/> test builder.</returns> public static TRedirectResultTestBuilder PreservingMethod <TRedirectResultTestBuilder>( this IBaseTestBuilderWithRedirectResult <TRedirectResultTestBuilder> baseTestBuilderWithRedirectResult, bool preserveMethod = true) where TRedirectResultTestBuilder : IBaseTestBuilderWithActionResult { var actualBuilder = GetActualBuilder(baseTestBuilderWithRedirectResult); RuntimeBinderValidator.ValidateBinding(() => { var actualPreserveMethod = actualBuilder .TestContext .MethodResult .AsDynamic() .PreserveMethod; if (preserveMethod != actualPreserveMethod) { actualBuilder.ThrowNewFailedValidationException( "to", $"{(preserveMethod ? string.Empty : "not ")}preserve the request method", $"in fact it did{(actualPreserveMethod ? string.Empty : " not")}"); } }); return(actualBuilder.ResultTestBuilder); }
/// <summary> /// Tests whether the redirect <see cref="Microsoft.AspNetCore.Mvc.ActionResult"/> is permanent. /// </summary> /// <param name="baseTestBuilderWithRedirectResult"> /// Instance of <see cref="IBaseTestBuilderWithRedirectResult{TRedirectResultTestBuilder}"/> type. /// </param> /// <param name="permanent">Expected boolean value.</param> /// <returns>The same redirect <see cref="Microsoft.AspNetCore.Mvc.ActionResult"/> test builder.</returns> public static TRedirectResultTestBuilder Permanent <TRedirectResultTestBuilder>( this IBaseTestBuilderWithRedirectResult <TRedirectResultTestBuilder> baseTestBuilderWithRedirectResult, bool permanent = true) where TRedirectResultTestBuilder : IBaseTestBuilderWithActionResult { var actualBuilder = GetActualBuilder(baseTestBuilderWithRedirectResult); RuntimeBinderValidator.ValidateBinding(() => { var actualPermanent = actualBuilder .TestContext .MethodResult .AsDynamic() .Permanent; if (permanent != actualPermanent) { actualBuilder.ThrowNewFailedValidationException( "to", $"{(permanent ? string.Empty : "not ")}be permanent", $"in fact it was{(actualPermanent ? string.Empty : " not")}"); } }); return(actualBuilder.ResultTestBuilder); }
/// <summary> /// Tests whether the <see cref="Microsoft.AspNetCore.Mvc.ViewResult"/> /// or the <see cref="Microsoft.AspNetCore.Mvc.PartialViewResult"/> /// has the same view name as the provided one. /// </summary> /// <param name="baseTestBuilderWithViewResult"> /// Instance of <see cref="IBaseTestBuilderWithViewResult{TViewResultTestBuilder}"/> type. /// </param> /// <param name="viewName">Expected view name.</param> /// <returns>The same <see cref="Microsoft.AspNetCore.Mvc.ActionResult"/> test builder.</returns> public static TViewResultTestBuilder WithName <TViewResultTestBuilder>( this IBaseTestBuilderWithViewResult <TViewResultTestBuilder> baseTestBuilderWithViewResult, string viewName) where TViewResultTestBuilder : IBaseTestBuilderWithActionResult { var actualBuilder = GetActualBuilder(baseTestBuilderWithViewResult); RuntimeBinderValidator.ValidateBinding(() => { var actualViewName = actualBuilder .TestContext .MethodResult .AsDynamic() ?.ViewName as string; if (viewName != actualViewName) { actualBuilder.ThrowNewFailedValidationException( "to", $"be {TestHelper.GetFriendlyName(viewName)}", $"instead received {TestHelper.GetFriendlyName(actualViewName)}"); } }); return(actualBuilder.ResultTestBuilder); }
/// <summary> /// Tests whether the <see cref="RedirectToActionResult"/> or <see cref="RedirectToRouteResult"/> /// has the same fragment as the provided one. /// </summary> /// <param name="redirectTestBuilder"> /// Instance of <see cref="IRedirectTestBuilder"/> type. /// </param> /// <param name="fragment">Expected fragment.</param> /// <returns>The same <see cref="IAndRedirectTestBuilder"/>.</returns> public static IAndRedirectTestBuilder WithFragment( this IRedirectTestBuilder redirectTestBuilder, string fragment) { var actualBuilder = (IBaseTestBuilderWithRedirectResultInternal <IAndRedirectTestBuilder>)redirectTestBuilder; RuntimeBinderValidator.ValidateBinding(() => { var actualFragment = actualBuilder .TestContext .MethodResult .AsDynamic() .Fragment; if (fragment != actualFragment) { actualBuilder.ThrowNewFailedValidationException( "fragment", $"to be {fragment.GetErrorMessageName()}", $"instead received {actualFragment.GetErrorMessageName()}"); } }); return(actualBuilder.ResultTestBuilder); }
/// <summary> /// Tests whether created result location passes given assertions. /// </summary> /// <param name="assertions">Action containing all assertions on the location.</param> /// <returns>The same created test builder.</returns> public IAndCreatedTestBuilder AtLocationPassing(Action <string> assertions) { RuntimeBinderValidator.ValidateBinding(() => { var location = (string)this.GetActionResultAsDynamic().Location.OriginalString; assertions(location); }); return(this); }
public void ValidateBindingShouldThrowExceptionWithInvalidPropertyCall() { var actionResultWithFormatters = new CreatedNegotiatedContentResult <int>( TestObjectFactory.GetUri(), 5, MyWebApi.Controller <WebApiController>().Controller); RuntimeBinderValidator.ValidateBinding(() => { var contentNegotiator = (actionResultWithFormatters as dynamic).ModelState; Assert.IsNotNull(contentNegotiator); }); }
public void ValidateBindingShouldNotThrowExceptionWithValidPropertyCall() { var actionResultWithProperty = new OkObjectResult("Test"); RuntimeBinderValidator.ValidateBinding(() => { var value = (actionResultWithProperty as dynamic).Value; Assert.NotNull(value); Assert.Equal("Test", value); }); }
/// <summary> /// Gets the view engine instance from a view result. /// </summary> /// <returns>Type of IViewEngine.</returns> protected IViewEngine GetViewEngine() { IViewEngine viewEngine = null; RuntimeBinderValidator.ValidateBinding(() => { viewEngine = this.GetActionResultAsDynamic().ViewEngine; }); return(viewEngine); }
/// <summary> /// Gets the view engine instance from a view result. /// </summary> /// <returns>Type of IViewEngine.</returns> protected IViewEngine GetViewEngine() { IViewEngine viewEngine = null; RuntimeBinderValidator.ValidateBinding(() => { viewEngine = this.TestContext.MethodResult.AsDynamic()?.ViewEngine; }); return(viewEngine); }
/// <summary> /// Gets the view engine instance from a view result. /// </summary> /// <returns>Type of <see cref="IViewEngine"/>.</returns> private static IViewEngine GetViewEngine <TViewResultTestBuilder>( IBaseTestBuilderWithViewResult <TViewResultTestBuilder> baseTestBuilderWithViewResult) where TViewResultTestBuilder : IBaseTestBuilderWithActionResult { var actualBuilder = (BaseTestBuilderWithComponent)baseTestBuilderWithViewResult; IViewEngine viewEngine = null; RuntimeBinderValidator.ValidateBinding(() => { viewEngine = actualBuilder.TestContext.MethodResult.AsDynamic()?.ViewEngine; }); return(viewEngine); }
private IAndCreatedTestBuilder AtRoute <TController>(LambdaExpression actionCall) where TController : ApiController { RuntimeBinderValidator.ValidateBinding(() => { var createdAtRouteResult = this.GetActionResultAsDynamic(); RouteValidator.Validate <TController>( createdAtRouteResult.Request, createdAtRouteResult.RouteName, createdAtRouteResult.RouteValues, actionCall, new Action <string, string, string>(this.ThrowNewCreatedResultAssertionException)); }); return(this); }
/// <inheritdoc /> public IAndRedirectTestBuilder Permanent() { RuntimeBinderValidator.ValidateBinding(() => { var dynamicActionResult = this.ActionResult.AsDynamic(); if (!dynamicActionResult.Permanent) { this.ThrowNewRedirectResultAssertionException( "to", "be permanent", "in fact it was not"); } }); return(this); }
public void ValidateBindingShouldThrowExceptionWithInvalidPropertyCall() { Test.AssertException <InvalidCallAssertionException>( () => { var actionResultWithProperty = new OkObjectResult("Test"); RuntimeBinderValidator.ValidateBinding(() => { var value = (actionResultWithProperty as dynamic).InvalidValue; Assert.NotNull(value); Assert.Equal("Test", value); }); }, "Expected result to contain 'InvalidValue' property to test, but in fact such property was not found."); }
/// <summary> /// Tests whether created result location passes given predicate. /// </summary> /// <param name="predicate">Predicate testing the location.</param> /// <returns>The same created test builder.</returns> public IAndCreatedTestBuilder AtLocationPassing(Func <string, bool> predicate) { RuntimeBinderValidator.ValidateBinding(() => { var location = (string)this.GetActionResultAsDynamic().Location.OriginalString; if (!predicate(location)) { this.ThrowNewCreatedResultAssertionException( "Location", "to pass the given predicate", "it failed"); } }); return(this); }
/// <summary> /// Tests whether content result has the same content type as the provided MediaTypeHeaderValue. /// </summary> /// <param name="mediaType">Media type as MediaTypeHeaderValue.</param> /// <returns>The same content test builder.</returns> public IAndContentTestBuilder WithMediaType(MediaTypeHeaderValue mediaType) { RuntimeBinderValidator.ValidateBinding(() => { var actualMediaType = this.GetActionResultAsDynamic().MediaType as MediaTypeHeaderValue; if ((mediaType == null && actualMediaType != null) || (mediaType != null && actualMediaType == null) || (mediaType != null && mediaType.MediaType != actualMediaType.MediaType)) { this.ThrowNewContentResultAssertionException( "MediaType", string.Format("to be {0}", mediaType != null ? mediaType.MediaType : "null"), string.Format("instead received {0}", actualMediaType != null ? actualMediaType.MediaType : "null")); } }); return(this); }
/// <summary> /// Tests whether JSON result has JSON serializer settings by using builder. /// </summary> /// <param name="jsonSerializerSettingsBuilder">Builder for creating JSON serializer settings.</param> /// <returns>The same JSON test builder.</returns> public IAndJsonTestBuilder WithJsonSerializerSettings( Action <IJsonSerializerSettingsTestBuilder> jsonSerializerSettingsBuilder) { RuntimeBinderValidator.ValidateBinding(() => { var actualJsonSerializerSettings = this.GetActionResultAsDynamic().SerializerSettings as JsonSerializerSettings; var newJsonSerializerSettingsTestBuilder = new JsonSerializerSettingsTestBuilder(this.Controller, this.ActionName); jsonSerializerSettingsBuilder(newJsonSerializerSettingsTestBuilder); var expectedJsonSerializerSettings = newJsonSerializerSettingsTestBuilder.GetJsonSerializerSettings(); var validations = newJsonSerializerSettingsTestBuilder.GetJsonSerializerSettingsValidations(); validations.ForEach(v => v(expectedJsonSerializerSettings, actualJsonSerializerSettings)); }); return(this); }
/// <summary> /// Tests whether JSON result has the provided encoding. /// </summary> /// <param name="encoding">Expected encoding to test with.</param> /// <returns>The same JSON test builder.</returns> public IAndJsonTestBuilder WithEncoding(Encoding encoding) { RuntimeBinderValidator.ValidateBinding(() => { var actualEncoding = this.GetActionResultAsDynamic().Encoding as Encoding; if (!encoding.Equals(actualEncoding)) { throw new JsonResultAssertionException(string.Format( "When calling {0} action in {1} expected JSON result encoding to be {2}, but instead received {3}.", this.ActionName, this.Controller.GetName(), this.GetEncodingName(encoding), this.GetEncodingName(actualEncoding))); } }); return(this); }
/// <summary> /// Tests whether the <see cref="Microsoft.AspNetCore.Mvc.ActionResult"/> /// location passes the given assertions. /// </summary> /// <param name="baseTestBuilderWithRedirectResult"> /// Instance of <see cref="IBaseTestBuilderWithRedirectResult{TRedirectResultTestBuilder}"/> type. /// </param> /// <param name="assertions">Action containing all assertions for the location.</param> /// <returns>The same redirect <see cref="Microsoft.AspNetCore.Mvc.ActionResult"/> test builder.</returns> public static TRedirectResultTestBuilder ToUrlPassing <TRedirectResultTestBuilder>( this IBaseTestBuilderWithRedirectResult <TRedirectResultTestBuilder> baseTestBuilderWithRedirectResult, Action <string> assertions) where TRedirectResultTestBuilder : IBaseTestBuilderWithActionResult { var actualBuilder = GetActualBuilder(baseTestBuilderWithRedirectResult); RuntimeBinderValidator.ValidateBinding(() => { var url = actualBuilder .TestContext .MethodResult .AsDynamic() .Url; assertions(url); }); return(actualBuilder.ResultTestBuilder); }
/// <summary> /// Tests whether content result has the same status code as the provided HttpStatusCode. /// </summary> /// <param name="statusCode">HttpStatusCode enumeration.</param> /// <returns>The same content test builder.</returns> public IAndContentTestBuilder WithStatusCode(HttpStatusCode statusCode) { RuntimeBinderValidator.ValidateBinding(() => { var actualStatusCode = (HttpStatusCode)this.GetActionResultAsDynamic().StatusCode; if (actualStatusCode != statusCode) { throw new ContentResultAssertionException(string.Format( "When calling {0} action in {1} expected to have {2} ({3}) status code, but received {4} ({5}).", this.ActionName, this.Controller.GetName(), (int)statusCode, statusCode, (int)actualStatusCode, actualStatusCode)); } }); return(this); }
protected void ValidateOrder(int order) { if (order != 0) { RuntimeBinderValidator.ValidateBinding(() => { this.Attribute.AsDynamic().Order = order; this.Validations.Add((expected, actual) => { var expectedOrder = expected.AsDynamic().Order; var actualOrder = actual.AsDynamic().Order; if (expectedOrder != actualOrder) { this.FailedValidationAction( $"{this.ExceptionMessagePrefix}order of {expectedOrder}", $"in fact found {actualOrder}"); } }); }); } }