There is always something unexpected that happens or something that requires a complex mock setup or whatnot when testing web applications. This project aim at regrouping as many of those solutions as possible, ready to use, as well as multiple small utilities.
If your specific use-case is not covered, you can open an issue then even contribute it.
The packages follows semantic versioning and uses Nerdbank.GitVersioning under the hood to automate versioning based on Git commits.
All packages are available on https://www.nuget.org/profiles/ForEvolve.
To load all testing packages, reference the ForEvolve.Testing package:
Install-Package ForEvolve.Testingor
dotnet add package ForEvolve.TestingTo load individual packages, you can:
Install-Package ForEvolve.Testing.Core
Install-Package ForEvolve.Testing.AspNetCoreor
dotnet add package ForEvolve.Testing.Core
dotnet add package ForEvolve.Testing.AspNetCoreFor the pre-release packages, use the ForEvolve/Testing feedz.io packages source.
The repository is divided into multiple projects, with ForEvolve.Testing that references the others.
This project load all of the other projects to create "a NuGet package to rule them all"!
This project contains multiple test helpers.
- An extension on
KeyValuePair<string, object>tokeyValue.AssertEqual("some key", "some object")that assert the equalities. - An extension on
Streamtostream.ShouldEqual("some string")that read theStreamthenAssert.Equalits value against the specifiedexpectedResult.
services.AddSingletonMock<TService>()orservices.AddSingletonMock<TService>(mock => { /*some setup code*/ })that add a mock with a singleton lifetime to theIServiceCollection.services.AddScopedMock<TService>()orservices.AddScopedMock<TService>(mock => { /*some setup code*/ })that add a mock with a scoped lifetime to theIServiceCollection.services.AddTransientMock<TService>()orservices.AddTransientMock<TService>(mock => { /*some setup code*/ })that add a mock with a transient lifetime to theIServiceCollection.
serviceProvider.AssertServiceExists<TInterface>()thatGetRequiredService<TInterface>(), throwing an exception if the dependency does not exist.serviceProvider.AssertServiceImplementationExists<TInterface, TImplementation>()thatGetRequiredService<TInterface>(), throwing an exception if the dependency does not exist, then validate that its implementation is of typeTImplementation.serviceProvider.AssertServicesImplementationExists<TInterface, TImplementation>()thatGetServices<TInterface>(), make sure there is at least one implementation of typeTImplementation. Throw aTrueExceptionwhen no binding are of typeTImplementation.- Multiple other useful method to test the content of the
IServiceCollectionto ensure that dependencies are registered against the DI Container, under the expected scope. Moreover, those methods are chainable. For example, we could:services .AssertSingletonServiceExists<SomeService>() .AssertSingletonServiceImplementationExists<ISomeOtherService, DefaultSomeOtherService>() .AssertScopedServiceExists<SomeScopedService>() ;
This project contains some Asp.Net Core test utilities like:
IdentityHelperthat creates the plumbing to mockSignInManager<TUser>andUserManager<TUser>. Multiple authentication scenarios are supported.HttpContextHelperthat creates the plumbing to mockHttpContext,HttpRequest, andHttpResponse.MvcContextHelperat creates the plumbing to mockIUrlHelperandActionContext; including support forRouteData,ActionDescriptor, andModelStateDictionary.- An extension method on
HttpResponsetohttpResponse.BodyShouldEqual("Some value") - An extension on
WebApplicationFactory<TEntryPoint>towebApplicationFactory.FindServiceCollection()that returns the currentIServiceCollectionfrom the specifiedWebApplicationFactory; this is useful to assert service registration. - An extension on
IServiceCollectiontoservices.RemoveFilter<TFilter>()that remove the specified filter fromMvcOptions. The method rely onPostConfigure<MvcOptions>(...).
I started this project to share test utilities between projects and it now includes multiple testing utilities for .Net Core and Asp.Net Core.
The initial code comes from the ForEvolve.XUnit project, see ForEvolve-Framework.
There is a lot of stuff that I want to improve or add to this library in the future, I will do when I have the time to.
If you would like to contribute to the project, first, thank you for your interest and please read Contributing to ForEvolve open source projects for more information.
Also, please read the Contributor Covenant Code of Conduct that applies to all ForEvolve repositories.