Ejemplo n.º 1
0
 /// <inheritdoc />
 public IAndViewComponentBuilder <TViewComponent> WithActionContext(
     ActionContext actionContext)
 {
     CommonValidator.CheckForNullReference(actionContext, nameof(ActionContext));
     this.TestContext.ComponentContext = ViewContextMock.FromActionContext(this.TestContext, actionContext);
     return(this);
 }
Ejemplo n.º 2
0
 public void CheckForNullReferenceShouldThrowArgumentNullExceptionWithNullObject()
 {
     Assert.Throws <NullReferenceException>(() =>
     {
         CommonValidator.CheckForNullReference(null);
     });
 }
Ejemplo n.º 3
0
 /// <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;
 }
Ejemplo n.º 4
0
 /// <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;
        }
Ejemplo n.º 6
0
 /// <inheritdoc />
 public IAndViewComponentBuilder <TViewComponent> WithViewContext(
     ViewContext viewContext)
 {
     CommonValidator.CheckForNullReference(viewContext, nameof(ViewContext));
     this.TestContext.ComponentContext = ViewContextMock.FromViewContext(this.TestContext, viewContext);
     return(this);
 }
Ejemplo n.º 7
0
        /// <inheritdoc />
        public void For <TOptions>(Action <TOptions> optionsSetup)
            where TOptions : class, new()
        {
            CommonValidator.CheckForNullReference(optionsSetup, nameof(optionsSetup));

            optionsSetup(this.TestContext.GetOptions <TOptions>());
        }
Ejemplo n.º 8
0
        public DistributedCacheEntryTestBuilder(ComponentTestContext testContext)
        {
            CommonValidator.CheckForNullReference(testContext, nameof(ComponentTestContext));

            this.validations = new List <Action <DistributedCacheEntryMock, DistributedCacheEntryMock> >();
            this.TestContext = testContext;
        }
Ejemplo n.º 9
0
        /// <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>());
        }
Ejemplo n.º 10
0
 /// <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);
 }
Ejemplo n.º 11
0
        /// <inheritdoc />
        public IAndWithDbContextTestBuilder WithEntities <TDbContext>(Action <TDbContext> assertions) where TDbContext : DbContext
        {
            CommonValidator.CheckForNullReference(assertions, nameof(assertions));

            assertions(this.TestContext.GetDbContext <TDbContext>());

            return(this);
        }
Ejemplo n.º 12
0
        private static void RemoveServices(IServiceCollection serviceCollection, Func <ServiceDescriptor, bool> predicate)
        {
            CommonValidator.CheckForNullReference(serviceCollection, nameof(IServiceCollection));

            serviceCollection
            .Where(predicate)
            .ToArray()
            .ForEach(s => serviceCollection.Remove(s));
        }
Ejemplo n.º 13
0
        /// <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();
        }
Ejemplo n.º 14
0
        /// <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);
        }
Ejemplo n.º 19
0
        /// <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>();
        }
Ejemplo n.º 22
0
        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));
        }
Ejemplo n.º 23
0
        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();
        }
Ejemplo n.º 25
0
 /// <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);
        }
Ejemplo n.º 29
0
        /// <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));
        }
Ejemplo n.º 30
0
        /// <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()));
            }
        }