Skip to content

Commit

Permalink
Add more Decorate tests (#846)
Browse files Browse the repository at this point in the history
This adds more cases of fx.Decorate tests, specifically for
transitive dependencies that are decorated. For example, when a
function depends on type A whose constructor depends on type B,
and B has a decorator associated with it, we expect type A's constructor
to use the decorated value of B in its constructor.
  • Loading branch information
sywhang committed Feb 22, 2022
1 parent 7bb5b40 commit e279a3d
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions decorate_test.go
Expand Up @@ -213,6 +213,54 @@ func TestDecorateSuccess(t *testing.T) {
)
defer app.RequireStart().RequireStop()
})

t.Run("transitive decoration", func(t *testing.T) {
type Config struct {
Scope string
}
type Logger struct {
Cfg *Config
}
app := fxtest.New(t,
fx.Provide(func() *Config { return &Config{Scope: "root"} }),
fx.Module("child",
fx.Decorate(func() *Config { return &Config{Scope: "child"} }),
fx.Provide(func(cfg *Config) *Logger { return &Logger{Cfg: cfg} }),
fx.Invoke(func(l *Logger) {
assert.Equal(t, "child", l.Cfg.Scope)
}),
),
)
defer app.RequireStart().RequireStop()
})

t.Run("transitive and scoped decorations", func(t *testing.T) {
type Config struct {
Scope string
}
type Logger struct {
Cfg *Config
}
app := fxtest.New(t,
fx.Provide(func() *Config {
return &Config{Scope: "root"}
}),
fx.Provide(func(cfg *Config) *Logger {
return &Logger{Cfg: &Config{
Scope: cfg.Scope + " logger",
}}
}),
fx.Module("child",
fx.Decorate(func() *Config {
return &Config{Scope: "child"}
}),
fx.Invoke(func(l *Logger) {
assert.Equal(t, "child logger", l.Cfg.Scope)
}),
),
)
defer app.RequireStart().RequireStop()
})
}

func TestDecorateFailure(t *testing.T) {
Expand Down

0 comments on commit e279a3d

Please sign in to comment.