Class WebHostTestFactory
- Assembly
- Codebelt.Extensions.Xunit.Hosting.AspNetCore.dll
Provides a set of static methods for ASP.NET Core (including, but not limited to MVC, Razor and related) unit testing.
public static class WebHostTestFactory
- Inheritance
-
ObjectWebHostTestFactory
Methods
Create(Action<IServiceCollection>, Action<IApplicationBuilder>, Action<IHostBuilder>)
Creates and returns an IWebHostTest implementation.
public static IWebHostTest Create(Action<IServiceCollection> serviceSetup = null, Action<IApplicationBuilder> pipelineSetup = null, Action<IHostBuilder> hostSetup = null)
Parameters
serviceSetup
Action<IServiceCollection>The Microsoft.Extensions.DependencyInjection.IServiceCollection which may be configured.
pipelineSetup
Action<IApplicationBuilder>The Microsoft.AspNetCore.Builder.IApplicationBuilder which may be configured.
hostSetup
Action<IHostBuilder>The Microsoft.Extensions.Hosting.IHostBuilder which may be configured.
Returns
- IWebHostTest
An instance of an IWebHostTest implementation.
CreateWithHostBuilderContext(Action<HostBuilderContext, IServiceCollection>, Action<HostBuilderContext, IApplicationBuilder>, Action<IHostBuilder>)
Creates and returns an IWebHostTest implementation.
public static IWebHostTest CreateWithHostBuilderContext(Action<HostBuilderContext, IServiceCollection> serviceSetup = null, Action<HostBuilderContext, IApplicationBuilder> pipelineSetup = null, Action<IHostBuilder> hostSetup = null)
Parameters
serviceSetup
Action<HostBuilderContext, IServiceCollection>The Microsoft.Extensions.DependencyInjection.IServiceCollection which may be configured.
pipelineSetup
Action<HostBuilderContext, IApplicationBuilder>The Microsoft.AspNetCore.Builder.IApplicationBuilder which may be configured.
hostSetup
Action<IHostBuilder>The Microsoft.Extensions.Hosting.IHostBuilder which may be configured.
Returns
- IWebHostTest
An instance of an IWebHostTest implementation.
RunAsync(Action<IServiceCollection>, Action<IApplicationBuilder>, Action<IHostBuilder>, Func<HttpClient, Task<HttpResponseMessage>>)
Runs a middleware and returns an System.Net.Http.HttpClient for making HTTP requests to the test server.
public static async Task<HttpResponseMessage> RunAsync(Action<IServiceCollection> serviceSetup = null, Action<IApplicationBuilder> pipelineSetup = null, Action<IHostBuilder> hostSetup = null, Func<HttpClient, Task<HttpResponseMessage>> responseFactory = null)
Parameters
serviceSetup
Action<IServiceCollection>The Microsoft.Extensions.DependencyInjection.IServiceCollection which may be configured.
pipelineSetup
Action<IApplicationBuilder>The Microsoft.AspNetCore.Builder.IApplicationBuilder which may be configured.
hostSetup
Action<IHostBuilder>The Microsoft.Extensions.Hosting.IHostBuilder which may be configured.
responseFactory
Func<HttpClient, Task<HttpResponseMessage>>The function delegate that creates a System.Net.Http.HttpResponseMessage from the System.Net.Http.HttpClient. Default is a GET request to the root URL ("/").
Returns
- Task<HttpResponseMessage>
A System.Threading.Tasks.Task that represents the asynchronous operation. The task result contains the System.Net.Http.HttpResponseMessage for the test server.
RunWithHostBuilderContextAsync(Action<HostBuilderContext, IServiceCollection>, Action<HostBuilderContext, IApplicationBuilder>, Action<IHostBuilder>, Func<HttpClient, Task<HttpResponseMessage>>)
Runs a filter/middleware test.
public static async Task<HttpResponseMessage> RunWithHostBuilderContextAsync(Action<HostBuilderContext, IServiceCollection> serviceSetup = null, Action<HostBuilderContext, IApplicationBuilder> pipelineSetup = null, Action<IHostBuilder> hostSetup = null, Func<HttpClient, Task<HttpResponseMessage>> responseFactory = null)
Parameters
serviceSetup
Action<HostBuilderContext, IServiceCollection>The Microsoft.Extensions.DependencyInjection.IServiceCollection which may be configured.
pipelineSetup
Action<HostBuilderContext, IApplicationBuilder>The Microsoft.AspNetCore.Builder.IApplicationBuilder which may be configured.
hostSetup
Action<IHostBuilder>The Microsoft.Extensions.Hosting.IHostBuilder which may be configured.
responseFactory
Func<HttpClient, Task<HttpResponseMessage>>The function delegate that creates a System.Net.Http.HttpResponseMessage from the System.Net.Http.HttpClient. Default is a GET request to the root URL ("/").
Returns
- Task<HttpResponseMessage>
A System.Threading.Tasks.Task that represents the asynchronous operation. The task result contains the System.Net.Http.HttpResponseMessage for the test server.