private void ValidateRouteInformation <TController>() where TController : ApiController { var expectedRouteValues = this.GetExpectedRouteInfo <TController>(); var actualRouteValues = this.GetActualRouteInfo(); if (!actualRouteValues.IsResolved) { this.ThrowNewRouteAssertionException(actual: actualRouteValues.UnresolvedError); } if (actualRouteValues.IsIgnored) { this.ThrowNewRouteAssertionException(actual: "it was ignored with StopRoutingHandler"); } if (Reflection.AreDifferentTypes(this.expectedRouteInfo.Controller, this.actualRouteInfo.Controller)) { this.ThrowNewRouteAssertionException(actual: string.Format( "instead matched {0}", actualRouteValues.Controller.ToFriendlyTypeName())); } if (expectedRouteValues.Action != actualRouteValues.Action) { this.ThrowNewRouteAssertionException(actual: string.Format( "instead matched {0} action", actualRouteValues.Action)); } expectedRouteValues.Arguments.ForEach(arg => { if (arg.Value.Value != null && arg.Value.Value.ToString() == ExpressionParser.IgnoredArgument) { return; } if (!actualRouteValues.ActionArguments.ContainsKey(arg.Key)) { this.ThrowNewRouteAssertionException(actual: string.Format( "the '{0}' parameter could not be found", arg.Key)); } var expectedArgumentInfo = arg.Value; var actualArgumentInfo = actualRouteValues.ActionArguments[arg.Key]; if (Reflection.AreNotDeeplyEqual(expectedArgumentInfo.Value, actualArgumentInfo.Value)) { this.ThrowNewRouteAssertionException(actual: string.Format( "the '{0}' parameter was different", arg.Key)); } }); }
public static void ValidateInvocationResult <TResult>(ComponentTestContext testContext, TResult model) { ValidateInvocationResultType <TResult>(testContext); if (Reflection.AreNotDeeplyEqual(model, testContext.MethodResult)) { throw ResponseModelAssertionException.From(testContext.ExceptionMessagePrefix); } testContext.Model = model; }
/// <inheritdoc /> public IAndModelDetailsTestBuilder <TActionResult> Result <TResponseModel>(TResponseModel model) { this.ValidateActionReturnType <TResponseModel>(); if (Reflection.AreNotDeeplyEqual(model, this.ActionResult)) { throw new ResponseModelAssertionException($"When calling {this.ActionName} action in {this.Controller.GetName()} expected the response model to be the given model, but in fact it was a different one."); } this.TestContext.Model = model; return(new ModelDetailsTestBuilder <TActionResult>(this.TestContext)); }
/// <inheritdoc /> public IAndDistributedCacheTestBuilder ContainingEntry(string key, byte[] value) { var actualValue = this.GetValue(key); if (Reflection.AreNotDeeplyEqual(value, actualValue)) { this.ThrowNewDataProviderAssertionException( DistributedCacheName, "to have entry with the given value", "in fact it was different"); } return(this); }
public static void ValidateInvocationResult <TResult>(ComponentTestContext testContext, TResult model, bool canBeAssignable = false) { if (!Reflection.IsAnonymousType(typeof(TResult))) { ValidateInvocationResultType <TResult>(testContext, canBeAssignable); } if (Reflection.AreNotDeeplyEqual(model, testContext.MethodResult, out var result)) { throw ResponseModelAssertionException.From(testContext.ExceptionMessagePrefix, result); } testContext.Model = model; }
/// <inheritdoc /> public override IAndMemoryCacheEntryTestBuilder WithValue(object value) { this.validations.Add((expected, actual) => { if (Reflection.AreNotDeeplyEqual(expected.Value, actual.Value)) { this.ThrowNewDataProviderAssertionException( "to have entry with the given value", "in fact it was different"); } }); return(base.WithValue(value)); }
/// <inheritdoc /> public IAndDataProviderEntryTestBuilder WithValue(object value) { this.validations.Add((actual) => { if (Reflection.AreNotDeeplyEqual(value, actual)) { this.ThrowNewDataProviderAssertionException( $"to have entry with '{this.entryKey}' key and the given value", "the value was different"); } }); return(this); }
/// <inheritdoc /> public IAndHttpResponseTestBuilder WithBody <TBody>(TBody body, string contentType, Encoding encoding) { var parsedBody = FormattersHelper.ReadFromStream <TBody>(this.httpResponse.Body, contentType, encoding); if (Reflection.AreNotDeeplyEqual(body, parsedBody)) { this.ThrowNewHttpResponseAssertionException( "body", "to be the given object", "in fact it was different"); } return(this.WithContentType(contentType)); }
/// <inheritdoc /> public IAndMemoryCacheTestBuilder ContainingEntry(object key, object value) { var actualValue = this.GetValue(key); if (Reflection.AreNotDeeplyEqual(value, actualValue, out var result)) { this.ThrowNewDataProviderAssertionException( MemoryCacheName, "to have entry with the given value", $"in fact it was different. {result}"); } return(this); }
public new IAndDistributedCacheEntryTestBuilder WithValue(byte[] value) { this.validations.Add((expected, actual) => { if (Reflection.AreNotDeeplyEqual(expected.Value, actual.Value)) { this.ThrowNewDataProviderAssertionException( $"to have an entry with '{this.EntryKey}' key and the given value", "in fact it was different"); } }); base.WithValue(value); return(this); }
/// <summary> /// Validates whether Properties are the same as the provided ones from action result containing such property. /// </summary> /// <param name="actionResult">Action result with Properties.</param> /// <param name="properties">Expected authentication properties.</param> /// <param name="failedValidationAction">Action to call in case of failed validation.</param> public static void ValidateAuthenticationProperties( dynamic actionResult, AuthenticationProperties properties, Action <string, string, string> failedValidationAction) { var actualProperties = (AuthenticationProperties)TryGetAuthenticationProperties(actionResult); if (Reflection.AreNotDeeplyEqual(properties, actualProperties, out var result)) { failedValidationAction( "authentication properties", "to be the same as the provided one", $"instead received different result. {result}"); } }
/// <inheritdoc /> public new IAndMemoryCacheEntryTestBuilder WithValue(object value) { this.validations.Add((expected, actual) => { if (Reflection.AreNotDeeplyEqual(expected.Value, actual.Value, out var result)) { this.ThrowNewDataProviderAssertionException( $"to have entry with '{this.MemoryCacheEntry.Key}' key and the given value", $"in fact it was different. {result}"); } }); base.WithValue(value); return(this); }
/// <inheritdoc /> public IAndResolvedRouteTestBuilder ToDataToken(string key, object value) { this.ToDataToken(key); var actualInfo = this.GetActualRouteInfo(); var routeValue = actualInfo.RouteData.DataTokens[key]; if (Reflection.AreNotDeeplyEqual(value, routeValue)) { this.ThrowNewRouteAssertionException( $"contain data token with '{key}' key and the provided value", $"the value was different"); } return(this); }
/// <inheritdoc /> public IAndDistributedCacheTestBuilder ContainingEntry(string key, byte[] value, DistributedCacheEntryOptions options) { this.ContainingEntry(key, value); this.distributedCacheMock.TryGetCacheEntryOptions(key, out var actualOptions); if (Reflection.AreNotDeeplyEqual(options, actualOptions)) { this.ThrowNewDataProviderAssertionException( DistributedCacheName, "to have entry with the given options", "in fact they were different"); } return(this); }
/// <inheritdoc /> public IAndModelDetailsTestBuilder <TModel> WithModel <TModel>(TModel expectedModel) { this.WithModelOfType <TModel>(); var actualModel = this.GetActualModel <TModel>(); if (Reflection.AreNotDeeplyEqual(expectedModel, actualModel)) { throw new ResponseModelAssertionException(string.Format( this.ErrorMessageFormat, this.TestContext.ExceptionMessagePrefix, typeof(TModel).ToFriendlyTypeName())); } this.TestContext.Model = actualModel; return(new ModelDetailsTestBuilder <TModel>(this.TestContext)); }
/// <summary> /// Testuje, či sa od spomenutého obsahu správy HTTP odošle hĺbkovo rovnaký objekt ako poskytnutý. /// </summary> /// <typeparam name="TResponseModel">Type of the response model.</typeparam> /// <param name="content">Actual HTTP content.</param> /// <param name="expectedModel">Expected model to be returned.</param> /// <param name="failedValidationAction">Action to call in case of failed validation.</param> /// <param name="failedResponseModelValidationAction">Function returning exception, in case of failed response model validation.</param> /// <returns>The actual HTTP response model.</returns> public static TResponseModel WithResponseModel <TResponseModel>( HttpContent content, TResponseModel expectedModel, Action <string, string, string> failedValidationAction, Func <string, string, ResponseModelAssertionException> failedResponseModelValidationAction) { var actualModel = GetActualContentModel <TResponseModel>( content, failedResponseModelValidationAction); if (Reflection.AreNotDeeplyEqual(expectedModel, actualModel)) { throw failedResponseModelValidationAction("be the given model", "in fact it was a different model"); } return(actualModel); }
/// <summary> /// Tests whether the <see cref="Microsoft.AspNetCore.Mvc.SignInResult"/> /// has the provided <see cref="ClaimsPrincipal"/>. /// </summary> /// <param name="signInTestBuilder"> /// Instance of <see cref="ISignInTestBuilder"/> type. /// </param> /// <param name="principal">Expected <see cref="ClaimsPrincipal"/>.</param> /// <returns>The same <see cref="IAndSignInTestBuilder"/>.</returns> public static IAndSignInTestBuilder WithPrincipal( this ISignInTestBuilder signInTestBuilder, ClaimsPrincipal principal) { var actualBuilder = (SignInTestBuilder)signInTestBuilder; var actualPrincipal = actualBuilder.ActionResult.Principal; if (Reflection.AreNotDeeplyEqual(principal, actualPrincipal, out var result)) { actualBuilder.ThrowNewFailedValidationException( "principal", "to be the same as the provided one", $"instead received different result. {result}"); } return(actualBuilder); }
public static void ValidateStringKeyAndValue( string name, IDictionary <string, object> dictionary, string key, object value, Action <string, string, string> failedValidationAction) { var entryExists = dictionary.ContainsKey(key); var actualValue = entryExists ? dictionary[key] : null; if (!entryExists || Reflection.AreNotDeeplyEqual(value, actualValue)) { failedValidationAction( name, $"to have entry with '{key}' key and the provided value", $"{(entryExists ? "the value was different" : "such was not found")}"); } }
/// <summary> /// Tests whether the <see cref="FileResult"/> /// has the same entity tag as the provided one. /// </summary> /// <param name="baseTestBuilderWithFileResult"> /// Instance of <see cref="IBaseTestBuilderWithFileResult{TFileResultTestBuilder}"/> type. /// </param> /// <param name="entityTag">Entity tag header value.</param> /// <returns>The same <see cref="FileResult"/> test builder.</returns> public static TFileResultTestBuilder WithEntityTag <TFileResultTestBuilder>( this IBaseTestBuilderWithFileResult <TFileResultTestBuilder> baseTestBuilderWithFileResult, EntityTagHeaderValue entityTag) where TFileResultTestBuilder : IBaseTestBuilderWithActionResult { var actualBuilder = GetActualBuilder(baseTestBuilderWithFileResult); var actualEntityTag = GetFileResult(actualBuilder).EntityTag; if (Reflection.AreNotDeeplyEqual(entityTag, actualEntityTag)) { actualBuilder.ThrowNewFailedValidationException( "entity tag", $"to be {entityTag.GetErrorMessageName()}", $"instead received {actualEntityTag.GetErrorMessageName()}"); } return(actualBuilder.ResultTestBuilder); }
/// <summary> /// Tests whether a deeply equal object to the provided one is returned from the invoked action. /// </summary> /// <typeparam name="TResponseModel">Type of the response model.</typeparam> /// <param name="expectedModel">Expected model to be returned.</param> /// <returns>Builder for testing the response model errors.</returns> public IModelDetailsTestBuilder <TResponseModel> WithResponseModel <TResponseModel>(TResponseModel expectedModel) { this.WithResponseModelOfType <TResponseModel>(); var actualModel = this.GetActualModel <TResponseModel>(); if (Reflection.AreNotDeeplyEqual(expectedModel, actualModel)) { throw new ResponseModelAssertionException(string.Format( "When calling {0} action in {1} expected response model {2} to be the given model, but in fact it was a different model.", this.ActionName, this.Controller.GetName(), typeof(TResponseModel).ToFriendlyTypeName())); } return(new ModelDetailsTestBuilder <TResponseModel>( this.Controller, this.ActionName, this.CaughtException, actualModel)); }
/// <summary> /// Tests whether a deeply equal object to the provided one is returned from the invoked method. /// </summary> /// <typeparam name="TModel">Type of the model.</typeparam> /// <param name="builder">Instance of <see cref="IBaseTestBuilderWithResponseModel"/> type.</param> /// <param name="model">Expected model to be returned from the action result.</param> /// <returns>Test builder of <see cref="IModelDetailsTestBuilder{TModel}"/>.</returns> public static IAndModelDetailsTestBuilder <TModel> WithModel <TModel>( this IBaseTestBuilderWithResponseModel builder, TModel model) { var actualBuilder = (BaseTestBuilderWithResponseModel)builder; actualBuilder.WithModelOfType <TModel>(); var actualModel = actualBuilder.GetActualModel <TModel>(); if (Reflection.AreNotDeeplyEqual(model, actualModel)) { throw new ResponseModelAssertionException(string.Format( actualBuilder.ErrorMessageFormat, actualBuilder.TestContext.ExceptionMessagePrefix, typeof(TModel).ToFriendlyTypeName())); } actualBuilder.TestContext.Model = actualModel; return(new ModelDetailsTestBuilder <TModel>(actualBuilder.TestContext)); }
/// <inheritdoc /> public IAndResolvedRouteTestBuilder ToRouteValue(string key, object value) { this.ToRouteValue(key); var actualInfo = this.GetActualRouteInfo(); object routeValue; if (actualInfo.ActionArguments.ContainsKey(key)) { routeValue = actualInfo.ActionArguments[key].Value; } else { routeValue = actualInfo.RouteData.Values[key]; } var invalid = false; if (value is string || routeValue is string) { invalid = value.ToString() != routeValue.ToString(); } else { invalid = Reflection.AreNotDeeplyEqual(value, routeValue); } if (invalid) { this.ThrowNewRouteAssertionException( $"contain route value with '{key}' key and the provided value", $"the value was different"); } return(this); }