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>()or- services.AddSingletonMock<TService>(mock => { /*some setup code*/ })that add a mock with a singleton lifetime to the- IServiceCollection.
- services.AddScopedMock<TService>()or- services.AddScopedMock<TService>(mock => { /*some setup code*/ })that add a mock with a scoped lifetime to the- IServiceCollection.
- services.AddTransientMock<TService>()or- services.AddTransientMock<TService>(mock => { /*some setup code*/ })that add a mock with a transient lifetime to the- IServiceCollection.
- serviceProvider.AssertServiceExists<TInterface>()that- GetRequiredService<TInterface>(), throwing an exception if the dependency does not exist.
- serviceProvider.AssertServiceImplementationExists<TInterface, TImplementation>()that- GetRequiredService<TInterface>(), throwing an exception if the dependency does not exist, then validate that its implementation is of type- TImplementation.
- serviceProvider.AssertServicesImplementationExists<TInterface, TImplementation>()that- GetServices<TInterface>(), make sure there is at least one implementation of type- TImplementation. Throw a- TrueExceptionwhen no binding are of type- TImplementation.
- 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 mock- SignInManager<TUser>and- UserManager<TUser>. Multiple authentication scenarios are supported.
- HttpContextHelperthat creates the plumbing to mock- HttpContext,- HttpRequest, and- HttpResponse.
- MvcContextHelperat creates the plumbing to mock- IUrlHelperand- ActionContext; including support for- RouteData,- ActionDescriptor, and- ModelStateDictionary.
- 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.