dotnet / dotnet/aspnetcore

AddJsonOptions is run after Startup.Configure in integration tests

Open
#35,923 1 comment 1 reaction 0 assignees View on GitHub
area-mvc feature-mvc-testing
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 10h
Merged PRs (30d)
281

Description

### Describe the bug
I thought that code that should be run at the end of the startup process can be placed at the end of `Startup.Configure()`.

That isn't true when running an integration test with `WebApplicationFactory`.

### To Reproduce
Minimal `Startup.cs`:
````cs
public void ConfigureServices(IServiceCollection services) {
services
.AddControllers()
.AddControllersAsServices()
.AddJsonOptions(o => { // <--- problem here
Debug.WriteLine("AddJsonOptions");
o.JsonSerializerOptions.Converters.Add(new MyJsonConverter());
});
}

public void Configure() {
// stuff...

// this is the VERY END of the startup process, so run special code
Debug.WriteLine("Startup Completed");
RunSpecialCodeAtTheEndOfTheStartupProcess();
}
````

That works as expected, and I get this in the debug console:
> AddJsonOptions
> Startup Completed

But when running an integration test with `WebApplicationFactory`, the `AddJsonOptions()` delegate is run AFTER `Configure()`. The debug console shows:
> Startup Completed
> AddJsonOptions

If the timing of the special code is important, this makes the test fail.

### Exceptions (if any)
n/a

### Further technical details
- framework: .NET SDK 5.0.400 d61950f9bf
- OS: ubuntu 20.04
- IDE: vscode 1.59.1

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.