[WIP] Decouple WebApplicationFactory from TestServer implementation #60370
Labels
api-suggestion
Early API idea and discussion, it is NOT ready for implementation
needs-area-label
Used by the dotnet-issue-labeler to label those issues which couldn't be triaged automatically
Background and Motivation
We have a long-standing popular issue tracking improvement of automated browser testing with real server. Part of the ask is to decouple the WebApplicationFactory from the TestServer implementation, as they're currently [tightly coupled] (
aspnetcore/src/Mvc/Mvc.Testing/src/WebApplicationFactory.cs
Line 29 in 2edcf3b
Unfortunately, the TestServer is also exposed in WebApplicationFactory via a public property Server. Hence, decoupling requires an API change proposed below.
The second change is exposing the initialization logic publicly. Without this change, customers had to call the
CreateClient()
API which would internally initialize the server but wouldn't use the returned HttpClient instance. This will make it more intuitive and avoid creating unnecessary objects. Here is a screenshot from a blogpost pointing out this odd usage pattern:With the proposed changes, customers can now simply call
server.Initialize()
instead which is more intuitive.Proposed API
New abstraction / interface to depend on instead of the TestServer implementation.
Usage / API change in WebApplicationFactory:
Usage Examples
Alternative Designs
Risks
The text was updated successfully, but these errors were encountered: