/
fx_test_app.go
75 lines (66 loc) · 2 KB
/
fx_test_app.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package golibtest
import (
"context"
"fmt"
"github.com/golibs-starter/golib/log"
"github.com/pkg/errors"
"go.uber.org/fx"
"go.uber.org/fx/fxtest"
"testing"
)
func RequireFxApp(options ...fx.Option) *fx.App {
app, err := StartFxApp(options...)
if err != nil {
panic(fmt.Errorf("error when setup test app: [%v]", err))
}
return app
}
func StartFxApp(options ...fx.Option) (*fx.App, error) {
// We need add RegisterHttpHandlerOnStartOpt here
// to ensure this will invoke at last
//options = append(options, RegisterHttpHandlerOnStartOpt())
app := fx.New(options...)
if err := app.Err(); err != nil {
return nil, errors.WithMessage(err, "fx.New failed")
}
if err := app.Start(context.Background()); err != nil {
return nil, errors.WithMessage(err, "Error when start fx app")
}
return app, nil
}
func RequireFxAppT(tb testing.TB, options ...fx.Option) *fx.App {
app, err := StartFxAppT(tb, options...)
if err != nil {
panic(fmt.Errorf("error when setup test app: [%v]", err))
}
return app
}
func StartFxAppT(tb testing.TB, options ...fx.Option) (*fx.App, error) {
options = append(
options,
// Wrap current logger with testing logger
WrapTestingLoggerOpt(tb),
// We need add RegisterHttpHandlerOnStartOpt here
// to ensure this will invoke at last
//RegisterHttpHandlerOnStartOpt(),
)
app := fxtest.New(tb, options...)
if err := app.Start(context.Background()); err != nil {
return nil, errors.WithMessage(err, "Error when start fxtest app")
}
return app.App, nil
}
func WrapTestingLoggerOpt(tb testing.TB) fx.Option {
return fx.Decorate(func(oldLogger log.Logger) (log.Logger, error) {
var err error
var testingLogger *log.TestingLogger
if defaultLogger, ok := oldLogger.(*log.ZapLogger); ok {
testingLogger = log.NewTestingLoggerFromDefault(tb, defaultLogger)
} else {
if testingLogger, err = log.NewTestingLogger(tb, &log.Options{CallerSkip: 2}); err != nil {
return nil, errors.WithMessage(err, "init testing logger failed")
}
}
return testingLogger, nil
})
}