Skip to content

Commit

Permalink
Add example for asp.net core webapplicationfactory to the docs
Browse files Browse the repository at this point in the history
  • Loading branch information
thomhurst committed Jan 21, 2025
1 parent 10c280a commit 1f25997
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions docs/docs/examples/aspnet.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
---
sidebar_position: 2
---

# ASP.NET Core Web App/Api

If you want to test a web app, you can utilise the Microsoft.Mvc.Testing packages to wrap your web app within an in-memory test server.

```csharp
public class WebAppFactory : WebApplicationFactory<Program>, IAsyncInitializer
{
public Task InitializeAsync()
{
// You can also override certain services here to mock things out
// Grab a reference to the server
// This forces it to initialize.
// By doing it within this method, it's thread safe.
// And avoids multiple initialisations from different tests if parallelisation is switched on
_ = Server;

return Task.CompletedTask;
}
}
```

This factory can then be injected into your tests, and whether you have one shared instance, or shared per class/assembly, or a new instance each time, is up to you!

The `IAsyncInitializer.InitializeAsync` method that you can see above will be called before your tests are invoked, so you know all the initialisation has already been done for you.

```csharp
public class MyTests
{
[ClassDataSource<WebAppFactory>(Shared = SharedType.PerTestSession)]
public required WebAppFactory WebAppFactory { get; init; }

[Test]
public async Task Test1()
{
var response = await HttpHelper.Get("/my/endpoint");

await Assert.That(response.StatusCode).IsEqualTo(HttpStatusCode.OK);
}
}
```

0 comments on commit 1f25997

Please sign in to comment.