-
Notifications
You must be signed in to change notification settings - Fork 803
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WebApplicationFactory<Program> can only be used with workaround in F# aspnet integration tests #18302
Comments
closing as i need to verify something else, sorry |
ok checked and this also doesnt work it seems or at least can be confusing for new users...
in F#, so probably we can reopen this one? |
Yeah, it's because Another way you can work around it—which I think is conceptually simpler anyway—is to simply expose a public higher-order In Program.fsmodule Program
open System
open Microsoft.AspNetCore.Builder
open Microsoft.Extensions.Hosting
let app configureBuilder =
let builder = WebApplication.CreateBuilder (Environment.GetCommandLineArgs ())
// Your normal setup logic goes here, before the call to configureBuilder.
ignore <| builder.Logging.AddConsole ()
ignore <| builder.Services.AddSingleton<SomeService1> ()
ignore <| builder.Services.AddSingleton<SomeService2> ()
// …etc.
ignore <| configureBuilder builder
let app = builder.Build ()
ignore <| app.MapGet ("/", Func<_> (fun () -> "Hello, world"))
app
(app ignore).Run () In your tests, you can just pass in a function that configures the builder however you want, overriding services (or not), etc., as needed: Tests.fslet myTest route x y z =
task {
use app = Program.app (fun builder ->
ignore <| builder.WebHost.UseTestServer ()
// ignore <| builder.Services. … whatever else you want to override before `builder.Build ()` is called.
)
do! app.StartAsync ()
use client = app.GetTestClient ()
// The rest of your test logic.
use! resp = client.GetAsync route
// Your assertions, etc…
} |
the only way to make use of integration tests for aspnetcore from F# code , is either use the old
Startup
class setup (not valid for minimal api), or use this setup which makes use of a "trick" of declaring both a type and a module right after forProgram
as else the Program static class is not visible for unit/integration tests in the minimal api form?then it can be used by
WebApplicationFactory<Program>
in F# unit testsThe text was updated successfully, but these errors were encountered: