Pinja.NetCore.WebApi.TestUtil
4.5.0
dotnet add package Pinja.NetCore.WebApi.TestUtil --version 4.5.0
NuGet\Install-Package Pinja.NetCore.WebApi.TestUtil -Version 4.5.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Pinja.NetCore.WebApi.TestUtil" Version="4.5.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Pinja.NetCore.WebApi.TestUtil" Version="4.5.0" />
<PackageReference Include="Pinja.NetCore.WebApi.TestUtil" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Pinja.NetCore.WebApi.TestUtil --version 4.5.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Pinja.NetCore.WebApi.TestUtil, 4.5.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Pinja.NetCore.WebApi.TestUtil@4.5.0
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Pinja.NetCore.WebApi.TestUtil&version=4.5.0
#tool nuget:?package=Pinja.NetCore.WebApi.TestUtil&version=4.5.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Test utilities for Net Core web api
This is lightweight wrapper and collection of useful tools to work with .Net Core isolated test host.
Examples
Example GET
[Fact]
public async Task WhenGetIsCalled_ThenAssertingItWorks()
{
await TestHost.Run<TestStartup>().Get("/returnthree/")
.ExpectStatusCode(HttpStatusCode.OK)
.WithContentOf<int>()
.Passing(
x => x.Should().Be(3));
}
Example POST
[Fact]
public async Task WhenPostIsCalled_ThenAssertingItWorks()
{
await TestHost.Run<TestStartup>().Post("/returnsame/", new DummyRequest { Value = "3" })
.ExpectStatusCode(HttpStatusCode.OK)
.WithContentOf<DummyRequest>()
.Passing(x => x.Value.Should().Be("3"));
}
Example StartUp classes
Standalone test startup
public class TestStartup
{
public TestStartup(IHostingEnvironment env)
{
}
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc(options => options.Filters.Add(new ValidateModelAttribute()));
services.AddNodeServices();
services.AddTransient<IPdfConvert, PdfConvert>();
services.AddTransient<IPdfStorage, GoogleCloudPdfStorage>();
services.AddTransient<IPdfQueue, PdfQueue>();
services.AddTransient<IErrorPages, ErrorPages>();
services.AddDbContext<PdfDataContext>(opt => opt.UseInMemoryDatabase());
services.AddSingleton<IPdfStorage, InMemoryPdfStorage>();
services.AddTransient<IPdfMerger, PdfMerger>();
services.AddHangfire(config => config.UseMemoryStorage());
services.Configure<AppSettings>(a => a.BaseUrl = "http://localhost:5000");
}
public void Configure(IApplicationBuilder app)
{
app.UseMiddleware<TestAuthenticationMiddlewareForApiKey>();
app.UseHangfireServer();
app.UseMvc();
}
}
With real Startup
public class TestStartup
{
private readonly Startup _original;
public TestStartup(IHostingEnvironment env)
{
_original = new Startup(env);
}
public void ConfigureServices(IServiceCollection services)
{
_original.ConfigureServices(services);
services
.RemoveService<IInvitationMailer>()
.AddSingleton(Substitute.For<IInvitationMailer>())
.RemoveService<ICurrentAuthenticatedUserFromExternal>()
.AddSingleton(Substitute.For<ICurrentAuthenticatedUserFromExternal>())
.RemoveService<ICurrentUserAccess>()
.AddTransient(_ =>
{
var currentUserAccessMock = Substitute.For<ICurrentUserAccess>();
currentUserAccessMock.CanManageGroup(Arg.Any<Guid>()).Returns(true);
currentUserAccessMock.CanManageApp(Arg.Any<Guid>()).Returns(true);
currentUserAccessMock.CanManageUser(Arg.Any<Guid>()).Returns(true);
currentUserAccessMock.CanManageApplications().Returns(true);
return currentUserAccessMock;
});
services
.RemoveService<AuthorizationDataContext>()
.RemoveService<DbContextOptions<AuthorizationDataContext>>()
services.Configure<TokenSettings>(x =>
{
x.Secret = "IsolatedTestStartupIsolatedTestStartupIsolatedTestStartup";
x.RefreshTokenSecret = "IsolatedTestStartupIsolatedTestStartupIsolatedTestStartup_Refresh";
x.InitialTokenSecret = "IsolatedTestStartupIsolatedTestStartupIsolatedTestStartup_Initial";
x.TokenExpiresSeconds = 120
});
}
public void Configure(IApplicationBuilder app)
{
app.UseMiddleware<TestAuthenticationMiddlewareForClientJwt>();
app.UseMvc();
}
}
Further
See complete list of examples from test project.
Mocking depencies from DI
host.MockSetup<IExternalDepency>(x => x.SomeCall(Arg.Is("abc")).Returns("3"));
Replacing depencies in test host
public void ConfigureServices(IServiceCollection services)
{
_original.ConfigureServices(services);
services.RemoveService<IExternalDepency>()
.AddSingleton(Substitute.For<IExternalDepency>());
}
Asserting and mocking
These utilities are framework independant. Use your favorite assertion and mocking libraries.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net10.0
- Microsoft.AspNetCore.Mvc.Testing (>= 10.0.3)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.3)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 4.5.0 | 55 | 3/18/2026 |
| 0.0.0-preview.2 | 33 | 3/18/2026 |