Skip to main content

Definition

Assembly: CloudNimble.Breakdance.AspNetCore.dll Namespace: CloudNimble.Breakdance.AspNetCore Inheritance: CloudNimble.Breakdance.AspNetCore.AspNetCoreBreakdanceTestBase

Syntax

CloudNimble.Breakdance.AspNetCore.AspNetCoreBreakdanceTestBase<TStartup>

Summary

A base class for building unit tests for AspNetCore APIs that automatically maintains a TestServer with configuration and a Dependency Injection containers for you.

Type Parameters

  • TStartup -

Constructors

.ctor

Creates a new AspNetCoreBreakdanceTestBase instance.

Syntax

public AspNetCoreBreakdanceTestBase()

Remarks

The call to .Configure() with no content is required to get a minimal, empty IWebHost.

.ctor Inherited

Inherited from CloudNimble.Breakdance.AspNetCore.AspNetCoreBreakdanceTestBase
Creates a new AspNetCoreBreakdanceTestBase instance.

Syntax

public AspNetCoreBreakdanceTestBase()

Remarks

Uses the modern IHostBuilder pattern for web hosting instead of the deprecated WebHostBuilder.

Properties

TestHostBuilder Inherited

Inherited from CloudNimble.Breakdance.AspNetCore.AspNetCoreBreakdanceTestBase
Replaces the AspNetCoreBreakdanceTestBase.TestHostBuilder from the BreakdanceTestBase with an IHostBuilder implementation configured for web hosting.

Syntax

public Microsoft.Extensions.Hosting.IHostBuilder TestHostBuilder { get; internal set; }

Property Value

Type: Microsoft.Extensions.Hosting.IHostBuilder

TestServer Inherited

Inherited from CloudNimble.Breakdance.AspNetCore.AspNetCoreBreakdanceTestBase
The AspNetCoreBreakdanceTestBase.TestServer for handling requests.

Syntax

public Microsoft.AspNetCore.TestHost.TestServer TestServer { get; internal set; }

Property Value

Type: Microsoft.AspNetCore.TestHost.TestServer

Methods

AddApis Inherited

Inherited from CloudNimble.Breakdance.AspNetCore.AspNetCoreBreakdanceTestBase
Adds Controller services to the AspNetCoreBreakdanceTestBase.TestHostBuilder.

Syntax

public void AddApis(System.Action<Microsoft.AspNetCore.Mvc.MvcOptions> options = null, System.Action<Microsoft.AspNetCore.Builder.IApplicationBuilder> app = null)

Parameters

NameTypeDescription
optionsSystem.Action<Microsoft.AspNetCore.Mvc.MvcOptions>-
appSystem.Action<Microsoft.AspNetCore.Builder.IApplicationBuilder>-

Remarks

Calls AddControllers() on the IServiceCollection which does the following, according to the Microsoft docs: combines the effects of Microsoft.Extensions.DependencyInjection.MvcCoreServiceCollectionExtensions.AddMvcCore(Microsoft.Extensions.DependencyInjection.IServiceCollection), Microsoft.Extensions.DependencyInjection.MvcApiExplorerMvcCoreBuilderExtensions.AddApiExplorer(Microsoft.Extensions.DependencyInjection.IMvcCoreBuilder), Microsoft.Extensions.DependencyInjection.MvcCoreMvcCoreBuilderExtensions.AddAuthorization(Microsoft.Extensions.DependencyInjection.IMvcCoreBuilder), Microsoft.Extensions.DependencyInjection.MvcCorsMvcCoreBuilderExtensions.AddCors(Microsoft.Extensions.DependencyInjection.IMvcCoreBuilder), Microsoft.Extensions.DependencyInjection.MvcDataAnnotationsMvcCoreBuilderExtensions.AddDataAnnotations(Microsoft.Extensions.DependencyInjection.IMvcCoreBuilder), and Microsoft.Extensions.DependencyInjection.MvcCoreMvcCoreBuilderExtensions.AddFormatterMappings(Microsoft.Extensions.DependencyInjection.IMvcCoreBuilder).

AddMinimalMvc Inherited

Inherited from CloudNimble.Breakdance.AspNetCore.AspNetCoreBreakdanceTestBase
Adds minimal MVC services to the AspNetCoreBreakdanceTestBase.TestHostBuilder.

Syntax

public void AddMinimalMvc(System.Action<Microsoft.AspNetCore.Mvc.MvcOptions> options = null, System.Action<Microsoft.AspNetCore.Builder.IApplicationBuilder> app = null)

Parameters

NameTypeDescription
optionsSystem.Action<Microsoft.AspNetCore.Mvc.MvcOptions>-
appSystem.Action<Microsoft.AspNetCore.Builder.IApplicationBuilder>-

Remarks

Calls AddMvcCore() on the IServiceCollection which does the following, according to the Microsoft docs: will register the minimum set of services necessary to route requests and invoke controllers. It is not expected that any application will satisfy its requirements with just a call to Microsoft.Extensions.DependencyInjection.MvcCoreServiceCollectionExtensions.AddMvcCore(Microsoft.Extensions.DependencyInjection.IServiceCollection). Additional configuration using the Microsoft.Extensions.DependencyInjection.IMvcCoreBuilder will be required.

AddRazorPages Inherited

Inherited from CloudNimble.Breakdance.AspNetCore.AspNetCoreBreakdanceTestBase
Adds support for Controllers and Razor views to the AspNetCoreBreakdanceTestBase.TestHostBuilder.

Syntax

public void AddRazorPages(System.Action<Microsoft.AspNetCore.Mvc.RazorPages.RazorPagesOptions> options = null, System.Action<Microsoft.AspNetCore.Builder.IApplicationBuilder> app = null)

Parameters

NameTypeDescription
optionsSystem.Action<Microsoft.AspNetCore.Mvc.RazorPages.RazorPagesOptions>-
appSystem.Action<Microsoft.AspNetCore.Builder.IApplicationBuilder>-

Remarks

Calls AddRazorPages() on the IServiceCollection which does the following, according to the Microsoft docs: combines the effects of Microsoft.Extensions.DependencyInjection.MvcCoreServiceCollectionExtensions.AddMvcCore(Microsoft.Extensions.DependencyInjection.IServiceCollection), Microsoft.Extensions.DependencyInjection.MvcCoreMvcCoreBuilderExtensions.AddAuthorization(Microsoft.Extensions.DependencyInjection.IMvcCoreBuilder), Microsoft.Extensions.DependencyInjection.MvcDataAnnotationsMvcCoreBuilderExtensions.AddDataAnnotations(Microsoft.Extensions.DependencyInjection.IMvcCoreBuilder), Microsoft.Extensions.DependencyInjection.TagHelperServicesExtensions.AddCacheTagHelper(Microsoft.Extensions.DependencyInjection.IMvcCoreBuilder), and Microsoft.Extensions.DependencyInjection.MvcRazorPagesMvcCoreBuilderExtensions.AddRazorPages(Microsoft.Extensions.DependencyInjection.IMvcCoreBuilder).

AddViews Inherited

Inherited from CloudNimble.Breakdance.AspNetCore.AspNetCoreBreakdanceTestBase
Adds support for Controllers and Razor views to the AspNetCoreBreakdanceTestBase.TestHostBuilder.

Syntax

public void AddViews(System.Action<Microsoft.AspNetCore.Mvc.MvcOptions> options = null, System.Action<Microsoft.AspNetCore.Builder.IApplicationBuilder> app = null)

Parameters

NameTypeDescription
optionsSystem.Action<Microsoft.AspNetCore.Mvc.MvcOptions>-
appSystem.Action<Microsoft.AspNetCore.Builder.IApplicationBuilder>-

Remarks

Calls AddControllersWithViews() on the IServiceCollection which does the following, according to the Microsoft docs: combines the effects of Microsoft.Extensions.DependencyInjection.MvcCoreServiceCollectionExtensions.AddMvcCore(Microsoft.Extensions.DependencyInjection.IServiceCollection), Microsoft.Extensions.DependencyInjection.MvcApiExplorerMvcCoreBuilderExtensions.AddApiExplorer(Microsoft.Extensions.DependencyInjection.IMvcCoreBuilder), Microsoft.Extensions.DependencyInjection.MvcCoreMvcCoreBuilderExtensions.AddAuthorization(Microsoft.Extensions.DependencyInjection.IMvcCoreBuilder), Microsoft.Extensions.DependencyInjection.MvcCorsMvcCoreBuilderExtensions.AddCors(Microsoft.Extensions.DependencyInjection.IMvcCoreBuilder), Microsoft.Extensions.DependencyInjection.MvcDataAnnotationsMvcCoreBuilderExtensions.AddDataAnnotations(Microsoft.Extensions.DependencyInjection.IMvcCoreBuilder), Microsoft.Extensions.DependencyInjection.MvcCoreMvcCoreBuilderExtensions.AddFormatterMappings(Microsoft.Extensions.DependencyInjection.IMvcCoreBuilder), Microsoft.Extensions.DependencyInjection.TagHelperServicesExtensions.AddCacheTagHelper(Microsoft.Extensions.DependencyInjection.IMvcCoreBuilder), Microsoft.Extensions.DependencyInjection.MvcViewFeaturesMvcCoreBuilderExtensions.AddViews(Microsoft.Extensions.DependencyInjection.IMvcCoreBuilder), and Microsoft.Extensions.DependencyInjection.MvcRazorMvcCoreBuilderExtensions.AddRazorViewEngine(Microsoft.Extensions.DependencyInjection.IMvcCoreBuilder).

AssemblySetup Override

Inherited from CloudNimble.Breakdance.AspNetCore.AspNetCoreBreakdanceTestBase
Method used by test assemblies to setup the environment.

Syntax

public override void AssemblySetup()

Remarks

With MSTest, use [AssemblyInitialize]. With NUnit, use [OneTimeSetup]. With xUnit, good luck: https://xunit.net/docs/shared-context

AssemblySetupAsync Inherited Virtual

Inherited from CloudNimble.Breakdance.AspNetCore.AspNetCoreBreakdanceTestBase
Method used by test assemblies to setup the environment asynchronously.

Syntax

public virtual System.Threading.Tasks.Task AssemblySetupAsync()

Returns

Type: System.Threading.Tasks.Task

Remarks

With MSTest, use [AssemblyInitialize]. With NUnit, use [OneTimeSetUp]. With xUnit, good luck: https://xunit.net/docs/shared-context

EnsureTestServer Inherited

Inherited from CloudNimble.Breakdance.AspNetCore.AspNetCoreBreakdanceTestBase
Ensures that the AspNetCoreBreakdanceTestBase.TestServer has been constructed.

Syntax

internal void EnsureTestServer()

Remarks

Builds the host using IHostBuilder, starts it, and retrieves the AspNetCoreBreakdanceTestBase.TestServer from the host services.

EnsureTestServerAsync Inherited

Inherited from CloudNimble.Breakdance.AspNetCore.AspNetCoreBreakdanceTestBase
Ensures that the AspNetCoreBreakdanceTestBase.TestServer has been constructed asynchronously.

Syntax

internal System.Threading.Tasks.Task EnsureTestServerAsync()

Returns

Type: System.Threading.Tasks.Task

Remarks

Builds the host using IHostBuilder, starts it, and retrieves the AspNetCoreBreakdanceTestBase.TestServer from the host services.

GetHttpClient Inherited

Inherited from CloudNimble.Breakdance.AspNetCore.AspNetCoreBreakdanceTestBase
Retrieves an HttpClient instance from the AspNetCoreBreakdanceTestBase.TestServer and properly configures the BaseAddress.

Syntax

public System.Net.Http.HttpClient GetHttpClient(string routePrefix = "api/tests/")

Parameters

NameTypeDescription
routePrefixstringThe string to append to the BaseAddress for all requests. Defaults to WebApiConstants.RoutePrefix.

Returns

Type: System.Net.Http.HttpClient A properly configured HttpClientinstance from the AspNetCoreBreakdanceTestBase.TestServer.

GetHttpClient Inherited

Inherited from CloudNimble.Breakdance.AspNetCore.AspNetCoreBreakdanceTestBase
Retrieves an HttpClient instance from the AspNetCoreBreakdanceTestBase.TestServer and properly configures the BaseAddress.

Syntax

public System.Net.Http.HttpClient GetHttpClient(System.Net.Http.Headers.AuthenticationHeaderValue authHeader, string routePrefix = "api/tests/")

Parameters

NameTypeDescription
authHeaderSystem.Net.Http.Headers.AuthenticationHeaderValue-
routePrefixstringThe string to append to the BaseAddress for all requests. Defaults to WebApiConstants.RoutePrefix.

Returns

Type: System.Net.Http.HttpClient A properly configured HttpClientinstance from the AspNetCoreBreakdanceTestBase.TestServer.

GetKeyedService Override

Inherited from CloudNimble.Breakdance.AspNetCore.AspNetCoreBreakdanceTestBase
Get service of type T from the System.IServiceProvider.

Syntax

public override T GetKeyedService<T>(string key) where T : class

Parameters

NameTypeDescription
keystringThe key of the service to get.

Returns

Type: T A service object of type T.

Type Parameters

  • T - The type of service object to get.

GetKeyedServices Override

Inherited from CloudNimble.Breakdance.AspNetCore.AspNetCoreBreakdanceTestBase
Get services of type T from the System.IServiceProvider.

Syntax

public override System.Collections.Generic.IEnumerable<T> GetKeyedServices<T>(string key) where T : class

Parameters

NameTypeDescription
keystringThe

Returns

Type: System.Collections.Generic.IEnumerable<T> An IEnumerable`1 of type T.

Type Parameters

  • T - The type of service object to get.

GetService Override

Inherited from CloudNimble.Breakdance.AspNetCore.AspNetCoreBreakdanceTestBase
Get service of type T from the System.IServiceProvider.

Syntax

public override T GetService<T>() where T : class

Returns

Type: T A service object of type T.

Type Parameters

  • T - The type of service object to get.

GetServices Override

Inherited from CloudNimble.Breakdance.AspNetCore.AspNetCoreBreakdanceTestBase
Get an enumeration of services of type T from the System.IServiceProvider.

Syntax

public override System.Collections.Generic.IEnumerable<T> GetServices<T>() where T : class

Returns

Type: System.Collections.Generic.IEnumerable<T> An enumeration of services of type T.

Type Parameters

  • T - The type of service object to get.

TestSetup Override

Inherited from CloudNimble.Breakdance.AspNetCore.AspNetCoreBreakdanceTestBase
Method used by test classes to setup the environment.

Syntax

public override void TestSetup()

Remarks

With MSTest, use [TestInitialize]. With NUnit, use [Setup]. With xUnit, good luck: https://xunit.net/docs/shared-context

TestSetupAsync Inherited Virtual

Inherited from CloudNimble.Breakdance.AspNetCore.AspNetCoreBreakdanceTestBase
Method used by test classes to setup the environment asynchronously.

Syntax

public virtual System.Threading.Tasks.Task TestSetupAsync()

Returns

Type: System.Threading.Tasks.Task

Remarks

With MSTest, use [TestInitialize]. With NUnit, use [SetUp]. With xUnit, good luck: https://xunit.net/docs/shared-context

TestTearDown Override

Inherited from CloudNimble.Breakdance.AspNetCore.AspNetCoreBreakdanceTestBase
Method used by test classes to clean up the environment.

Syntax

public override void TestTearDown()

Remarks

With MSTest, use [TestCleanup]. With NUnit, use [TearDown]. With xUnit, good luck: https://xunit.net/docs/shared-context