BufTools.AspNet.InMemoryEndpointTesting
1.0.3
Prefix Reserved
dotnet add package BufTools.AspNet.InMemoryEndpointTesting --version 1.0.3
NuGet\Install-Package BufTools.AspNet.InMemoryEndpointTesting -Version 1.0.3
<PackageReference Include="BufTools.AspNet.InMemoryEndpointTesting" Version="1.0.3" />
paket add BufTools.AspNet.InMemoryEndpointTesting --version 1.0.3
#r "nuget: BufTools.AspNet.InMemoryEndpointTesting, 1.0.3"
// Install BufTools.AspNet.InMemoryEndpointTesting as a Cake Addin #addin nuget:?package=BufTools.AspNet.InMemoryEndpointTesting&version=1.0.3 // Install BufTools.AspNet.InMemoryEndpointTesting as a Cake Tool #tool nuget:?package=BufTools.AspNet.InMemoryEndpointTesting&version=1.0.3
AspNet In-Memory Endpoint Testing
This lets you easily startup your ASP WebApi in-memory from a unit test, call endpoints, and get a response back to Assert on.
It allows injecting mocks, is ultra fast, and all with two lines of code!
example:
var browser = new Browser<Program>(c =>
{
c.UseDependency(_userServiceMock.Object);
c.UseDependency(_validationMock.Object);
});
var result = await browser.CreateRequest("/api/v1/example")
.WithQueryParam("string_param", myString)
.WithQueryParam("int_param", myInt)
.GetAsync();
Getting Started
The general approach is to:
- Create an instance of a Browser class with any mocks to inject.
- Use the browser instance to build HTTP requests using fluent syntax and send them
Creating a browser
- When new'ing up a browser, you need to choose the program type that starts the application.
Options are:
- Program : choose this if not using a Startup.cs file
- Startup : chose this if using a Startup.cs file
*** If using minimal hosting model(ie, Program instead of Startup), then you must make the Program class accessible by the Test project. Do so by adding this the .csproj file of the application under test:
<ItemGroup>
<InternalsVisibleTo Include="YourApi.Tests" />
</ItemGroup>
- Inject dependencies
Use the configurator to inject classes in place of those thave have already been registered to act as replacements.
Example
Example usage with Progam as the type and two mocks injected
var browser = new Browser<Program>(c =>
{
c.UseDependency(_userServiceMock.Object);
c.UseDependency(_validationMock.Object);
});
Call an Endpoint
- To call an endpoint, start with CreateRequest and provide the route to the endpoint with no base url
- Use WithQueryParam to add any params that get appended to the route with a ?
- Call GetAsync (or Put, Post, Delete) to send the request and get a response back
var result = await browser.CreateRequest("/api/v1/example")
.WithQueryParam("string_param", myString)
.WithQueryParam("int_param", myInt)
.GetAsync();
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp3.1 is compatible. |
-
.NETCoreApp 3.1
- Microsoft.AspNetCore.Mvc.Testing (>= 3.1.32)
- Microsoft.AspNetCore.TestHost (>= 3.1.32)
- Microsoft.TestPlatform.TestHost (>= 17.9.0)
-
net6.0
- Microsoft.AspNetCore.Mvc.Testing (>= 6.0.29)
- Microsoft.AspNetCore.TestHost (>= 6.0.29)
- Microsoft.TestPlatform.TestHost (>= 17.9.0)
-
net7.0
- Microsoft.AspNetCore.Mvc.Testing (>= 7.0.2)
- Microsoft.AspNetCore.TestHost (>= 7.0.2)
- Microsoft.TestPlatform.TestHost (>= 17.9.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Added package signing