Open trivigy opened 4 years ago
Thank you for your support !
I don't have anything new planned for the library as it already does everything that I need. But if you need a new feature or if you see a bug, don't hesitate to create an issue.
@sarulabs definitely not hesitating. The only thing I ran into is that it is a little hard to unittest when mocking is needed. If you have encountered this than I would love some suggestions.
I don't unit test the parts of my code using di, and I'm not a big fan of mocking either. So it has never been a problem for me. But you are not the first mentioning this, and that led to a change in the behavior of the Builder.Add
method. Now it allows to redefine a service.
Once the container has been built it is not possible to alter its definitions. So if you want to mock a service, it should be done with the Builder.Add
and Builder.Set
methods before calling Builder.Build
.
It should be possible to organize your code to make it easy to add new definitions for in your tests. With something like this for example:
// app.go
type App struct {
Definitions []di.Def
}
func (app *App) Run() {
builder, _ := di.NewBuilder()
builder.Add(app.Definitions...)
ctn := builder.Build()
// do something with the container
}
// services/defs.go
var Definitions = []di.Def{/* the real definitions */}
// main.go
func main() {
app := App{Definitions: services.Definitions}
app.Run()
}
// feature_test.go
func TestFeature(t *testing.T) {
app := App{}
app.Definitions = append(app.Definitions, services.Definitions...)
app.Definitions = append(app.Definitions, di.Def{/* mocked definition */})
// test app
}
Yeah I figured exactly this part on my own. Was digging through the code and saw that add just writes to a map. Which mean it overwrites things. Wish this was documented or there was api method that reflected this point easily. Wouldn't have had to dig into the code. Thanks for the feedback though.
I updated the documentation: https://github.com/sarulabs/di/pull/22/commits/92a15dc990e412487c4d5885d401f964ea1bc911
Don't have much to add other than to share praise. Keep on the good work.