samber / do

⚙️ A dependency injection toolkit based on Go 1.18+ Generics.
https://pkg.go.dev/github.com/samber/do
MIT License
1.82k stars 75 forks source link

Warm up the container #35

Closed jozolam closed 11 months ago

jozolam commented 1 year ago

Hello, I would like to test that all services are wired correctly inside my DIC and also warm up when booting the app. I would like to do something like this

func TestAllServicesInDI(t *testing.T) {
    injector := do.New()
    do.ProvideNamedValue(injector, "first", 1)
    do.ProvideNamedValue(injector, "second", 2)
    do.ProvideNamedValue(injector, "third", 3)
    do.ProvideNamedValue(injector, "string", "Hello World!")

    for _, v := range injector.ListProvidedServices() {
        _, err := do.InvokeNamed[any](injector, v)
        assert.NilError(t, err)
    }
}

It will fail with assertion failed: error is not nil: DI: could not find service "third", available services: "third", "string", "first", "second"

The easiest way how to accomplish this would be to add InvokeNamedUntyped(i, name) or something that would return any but invoke the service. WDYT?

samber commented 11 months ago

Hi there and sorry for the late reply.

It could be fixed by injecting the right type: do.InvokeNamed[int](injector, v)