/// <inheritdoc /> public IAndViewComponentBuilder <TViewComponent> WithActionContext( ActionContext actionContext) { CommonValidator.CheckForNullReference(actionContext, nameof(ActionContext)); this.TestContext.ComponentContext = ViewContextMock.FromActionContext(this.TestContext, actionContext); return(this); }
public void CheckForNullReferenceShouldThrowArgumentNullExceptionWithNullObject() { Assert.Throws <NullReferenceException>(() => { CommonValidator.CheckForNullReference(null); }); }
/// <summary> /// Initializes a new instance of the <see cref="HttpHandlerResponseMessageTestBuilder" /> class. /// </summary> /// <param name="httpResponseMessage">HTTP response result from the tested handler.</param> public HttpHandlerResponseMessageTestBuilder( HttpResponseMessage httpResponseMessage) : base() { CommonValidator.CheckForNullReference(httpResponseMessage, errorMessageName: "HttpResponseMessage"); this.httpResponseMessage = httpResponseMessage; }
/// <summary> /// Removes a scoped service from the <see cref="IServiceCollection"/>. /// </summary> /// <param name="serviceCollection">Instance of <see cref="IServiceCollection"/> type.</param> /// <param name="service">Type of the service which will be removed.</param> /// <param name="implementationType">Service implementation type which will be removed.</param> /// <returns>The same <see cref="IServiceCollection"/>.</returns> public static IServiceCollection RemoveScoped(this IServiceCollection serviceCollection, Type service, Type implementationType) { CommonValidator.CheckForNullReference(service, nameof(service)); CommonValidator.CheckForNullReference(implementationType, nameof(implementationType)); RemoveServices(serviceCollection, s => s.ServiceType == service && s.ImplementationType == implementationType && s.Lifetime == ServiceLifetime.Scoped); return(serviceCollection); }
/// <summary> /// Initializes a new instance of the <see cref="MemoryCacheEntryTestBuilder"/> class. /// </summary> /// <param name="testContext"><see cref="ComponentTestContext"/> containing data about the currently executed assertion chain.</param> public MemoryCacheEntryTestBuilder(ComponentTestContext testContext) { CommonValidator.CheckForNullReference(testContext, nameof(ComponentTestContext)); this.validations = new List <Action <ICacheEntry, ICacheEntry> >(); this.TestContext = testContext; }
/// <inheritdoc /> public IAndViewComponentBuilder <TViewComponent> WithViewContext( ViewContext viewContext) { CommonValidator.CheckForNullReference(viewContext, nameof(ViewContext)); this.TestContext.ComponentContext = ViewContextMock.FromViewContext(this.TestContext, viewContext); return(this); }
/// <inheritdoc /> public void For <TOptions>(Action <TOptions> optionsSetup) where TOptions : class, new() { CommonValidator.CheckForNullReference(optionsSetup, nameof(optionsSetup)); optionsSetup(this.TestContext.GetOptions <TOptions>()); }
public DistributedCacheEntryTestBuilder(ComponentTestContext testContext) { CommonValidator.CheckForNullReference(testContext, nameof(ComponentTestContext)); this.validations = new List <Action <DistributedCacheEntryMock, DistributedCacheEntryMock> >(); this.TestContext = testContext; }
/// <inheritdoc /> public void WithSet <TDbContext, TEntity>(Action <DbSet <TEntity> > assertions) where TDbContext : DbContext where TEntity : class { CommonValidator.CheckForNullReference(assertions, nameof(assertions)); assertions(this.TestContext.GetDbContext <TDbContext>().Set <TEntity>()); }
/// <summary> /// Initializes a new instance of the <see cref="BaseDataProviderTestBuilder"/> class. /// </summary> /// <param name="testContext"><see cref="ControllerTestContext"/> containing data about the currently executed assertion chain.</param> /// <param name="dataProviderName">Name of the data provider.</param> protected BaseDataProviderTestBuilder(ControllerTestContext testContext, string dataProviderName) : base(testContext) { CommonValidator.CheckForNotWhiteSpaceString(dataProviderName); this.DataProviderName = dataProviderName; this.DataProvider = this.GetDataProvider(); CommonValidator.CheckForNullReference(this.DataProvider); }
/// <inheritdoc /> public IAndWithDbContextTestBuilder WithEntities <TDbContext>(Action <TDbContext> assertions) where TDbContext : DbContext { CommonValidator.CheckForNullReference(assertions, nameof(assertions)); assertions(this.TestContext.GetDbContext <TDbContext>()); return(this); }
private static void RemoveServices(IServiceCollection serviceCollection, Func <ServiceDescriptor, bool> predicate) { CommonValidator.CheckForNullReference(serviceCollection, nameof(IServiceCollection)); serviceCollection .Where(predicate) .ToArray() .ForEach(s => serviceCollection.Remove(s)); }
/// <summary> /// Initializes a new instance of the <see cref="HttpContextMock"/> class by copying the properties from the provided one. /// </summary> /// <param name="context">HttpContext to copy properties from.</param> private HttpContextMock(HttpContext context) : this(context.Features) { CommonValidator.CheckForNullReference(context, nameof(HttpContext)); this.PrepareDefaultFeatures(); this.PrepareData(context); this.PrepareDefaultValues(); }
/// <summary> /// Adds view component testing services. /// </summary> /// <param name="serviceCollection">Instance of <see cref="IServiceCollection"/> type.</param> /// <returns>The same <see cref="IServiceCollection"/>.</returns> public static IServiceCollection AddViewComponentsTesting(this IServiceCollection serviceCollection) { CommonValidator.CheckForNullReference(serviceCollection, nameof(serviceCollection)); serviceCollection.TryAddSingleton <IViewComponentPropertyActivator, ViewComponentPropertyActivator>(); serviceCollection.TryAddSingleton <IViewComponentDescriptorCache, ViewComponentDescriptorCache>(); return(serviceCollection); }
/// <summary> /// Adds controller testing services. /// </summary> /// <param name="serviceCollection">Instance of <see cref="IServiceCollection"/> type.</param> /// <returns>The same <see cref="IServiceCollection"/>.</returns> public static IServiceCollection AddControllersTesting(this IServiceCollection serviceCollection) { CommonValidator.CheckForNullReference(serviceCollection, nameof(serviceCollection)); serviceCollection.TryAddSingleton <IValidControllersCache, ValidControllersCache>(); serviceCollection.Configure <MvcOptions>(options => options.Conventions.Add(new ValidControllersCache())); return(serviceCollection); }
/// <inheritdoc /> public void WithEntities <TDbContext>(Action <TDbContext> dbContextSetup) where TDbContext : DbContext { CommonValidator.CheckForNullReference(dbContextSetup, nameof(dbContextSetup)); var dbContext = this.TestContext.GetDbContext <TDbContext>(); dbContextSetup(dbContext); dbContext.SaveChanges(); }
/// <summary> /// Adds core MVC testing services. /// </summary> /// <param name="serviceCollection">Instance of <see cref="IServiceCollection"/> type.</param> /// <returns>The same <see cref="IServiceCollection"/>.</returns> public static IServiceCollection AddMvcCoreTesting(this IServiceCollection serviceCollection) { CommonValidator.CheckForNullReference(serviceCollection, nameof(serviceCollection)); serviceCollection .AddCoreTesting() .AddControllersTesting(); return serviceCollection; }
/// <summary> /// Adds view features testing services. /// </summary> /// <param name="serviceCollection">Instance of <see cref="IServiceCollection"/> type.</param> /// <returns>The same <see cref="IServiceCollection"/>.</returns> public static IServiceCollection AddViewFeaturesTesting(this IServiceCollection serviceCollection) { CommonValidator.CheckForNullReference(serviceCollection, nameof(serviceCollection)); serviceCollection .AddViewComponentsTesting() .ReplaceTempDataProvider(); return(serviceCollection); }
/// <inheritdoc /> public IAndServicesBuilder WithSetupFor <TService>(Action <TService> scopedServiceSetup) where TService : class { CommonValidator.CheckForNullReference(scopedServiceSetup, nameof(scopedServiceSetup)); ServiceValidator.ValidateScopedServiceLifetime <TService>(nameof(WithSetupFor)); scopedServiceSetup(this.HttpContext.RequestServices.GetRequiredService <TService>()); return(this); }
/// <summary> /// Initializes a new instance of the <see cref="HttpRequestBuilder"/> class. /// </summary> public HttpRequestBuilder(HttpContext httpContext) { CommonValidator.CheckForNullReference(httpContext, nameof(HttpContext)); this.request = new HttpRequestMock(httpContext) { Scheme = HttpScheme.Http, Path = "/" }; }
/// <summary> /// Initializes a new instance of the <see cref="HttpRequestMock"/> class. /// </summary> public HttpRequestMock(HttpContext httpContext) { CommonValidator.CheckForNullReference(httpContext, nameof(this.HttpContext)); this.httpContext = httpContext; this.headerDictionary = new HeaderDictionary(); this.formFiles = new FormFileCollection(); this.cookieValues = new Dictionary <string, string>(); this.formValues = new Dictionary <string, StringValues>(); this.queryValues = new Dictionary <string, StringValues>(); }
public static ControllerContext FromActionContext(HttpTestContext testContext, ActionContext actionContext) { CommonValidator.CheckForNullReference(testContext, nameof(HttpTestContext)); CommonValidator.CheckForNullReference(actionContext, nameof(ActionContext)); actionContext.HttpContext = actionContext.HttpContext ?? testContext.HttpContext; actionContext.RouteData = actionContext.RouteData ?? testContext.RouteData ?? new RouteData(); actionContext.ActionDescriptor = actionContext.ActionDescriptor ?? new ControllerActionDescriptor(); return(new MockedControllerContext(testContext, actionContext)); }
public PropertyActivator( PropertyInfo propertyInfo, Func <TContext, object> valueAccessor) { CommonValidator.CheckForNullReference(propertyInfo, nameof(propertyInfo)); CommonValidator.CheckForNullReference(valueAccessor, nameof(valueAccessor)); this.PropertyInfo = propertyInfo; this.valueAccessor = valueAccessor; this.fastPropertySetter = PropertyHelper.MakeFastPropertySetter(propertyInfo); }
/// <inheritdoc /> public void WithSet <TDbContext, TEntity>(Action <DbSet <TEntity> > entitySetup) where TDbContext : DbContext where TEntity : class { CommonValidator.CheckForNullReference(entitySetup, nameof(entitySetup)); var dbContext = this.TestContext.GetDbContext <TDbContext>(); entitySetup(dbContext.Set <TEntity>()); dbContext.SaveChanges(); }
/// <summary> /// Initializes a new instance of the <see cref="HttpResponseMessageTestBuilder" /> class. /// </summary> /// <param name="handler">Tested HTTP message handler.</param> /// <param name="httpResponseMessage">HTTP response result from the tested handler.</param> /// <param name="requestUri">HTTP request Uri.</param> /// <param name="responseTime">Measured response time from the tested handler.</param> public HttpResponseMessageTestBuilder( HttpMessageHandler handler, HttpResponseMessage httpResponseMessage, Uri requestUri, TimeSpan responseTime) { CommonValidator.CheckForNullReference(httpResponseMessage, errorMessageName: "HttpResponseMessage"); this.handler = handler; this.httpResponseMessage = httpResponseMessage; this.requestUri = requestUri; this.responseTime = responseTime; }
/// <summary> /// Adds MVC testing services. /// </summary> /// <param name="serviceCollection">Instance of <see cref="IServiceCollection"/> type.</param> /// <returns>The same <see cref="IServiceCollection"/>.</returns> public static IServiceCollection AddMvcTesting(this IServiceCollection serviceCollection) { CommonValidator.CheckForNullReference(serviceCollection, nameof(serviceCollection)); serviceCollection .AddMvcCoreTesting() .AddViewFeaturesTesting() .AddStringInputFormatter() .ReplaceOptions(); return(serviceCollection); }
public static ViewComponentContextMock FromViewComponentContext(HttpTestContext testContext, ViewComponentContext viewComponentContext) { CommonValidator.CheckForNullReference(testContext, nameof(HttpTestContext)); CommonValidator.CheckForNullReference(viewComponentContext, nameof(ViewComponentContext)); viewComponentContext.ViewComponentDescriptor = viewComponentContext.ViewComponentDescriptor ?? new ViewComponentDescriptor(); viewComponentContext.ViewContext = ViewContextMock.FromViewContext(testContext, viewComponentContext.ViewContext ?? new ViewContext()); viewComponentContext.Arguments = viewComponentContext.Arguments ?? new Dictionary <string, object>(); viewComponentContext.HtmlEncoder = viewComponentContext.HtmlEncoder ?? HtmlEncoder.Default; return(new ViewComponentContextMock(testContext, viewComponentContext)); }
/// <summary> /// Adds all testing services available in the testing framework. /// </summary> /// <param name="serviceCollection">Instance of <see cref="IServiceCollection"/> type.</param> /// <returns>The same <see cref="IServiceCollection"/>.</returns> public static IServiceCollection AddMvcUniverseTesting(this IServiceCollection serviceCollection) { CommonValidator.CheckForNullReference(serviceCollection, nameof(serviceCollection)); serviceCollection .AddMvcTesting() .ReplaceDbContext() .ReplaceMemoryCache() .ReplaceSession(); return(serviceCollection); }
/// <summary> /// Starts a route test. /// </summary> /// <param name="httpConfiguration">Optional HttpConfiguration to use in case one is not configured globally.</param> /// <returns>Route test builder.</returns> public static IRouteTestBuilder Routes(HttpConfiguration httpConfiguration = null) { if (httpConfiguration == null) { CommonValidator.CheckForNullReference( Configuration, "'IsUsing' method should be called before testing routes or HttpConfiguration should be provided. MyWebApi must be configured and HttpConfiguration"); httpConfiguration = Configuration; } return(new RouteTestBuilder(httpConfiguration)); }
/// <inheritdoc /> public void WithEntities <TDbContext>(Func <TDbContext, bool> predicate) where TDbContext : DbContext { CommonValidator.CheckForNullReference(predicate, nameof(predicate)); if (!predicate(this.TestContext.GetDbContext <TDbContext>())) { throw new DataProviderAssertionException(string.Format( "When calling {0} action in {1} expected the {2} entities to pass the given predicate, but it failed.", this.TestContext.ActionName, this.TestContext.Controller.GetName(), typeof(TDbContext).ToFriendlyTypeName())); } }