Comments (5)
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
}
from di.
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.
from di.
@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.
from di.
from di.
I updated the documentation:
92a15dc
from di.
Related Issues (15)
- Outside parameters for definition HOT 1
- Best way to go about passing/getting data from main package to another? HOT 1
- When using app.subcontainer is each one executed in it's own goroutine? HOT 2
- is it possible to Remove checkname from Add method ? HOT 12
- How to use it in Gin HOT 2
- HTTPMiddleware returns http.HandlerFunc HOT 2
- Http.Request and http.responsewriter in container HOT 1
- Is there a concept of Transient HOT 2
- Feature Request: Add by Type, GetByType and GetManyByType HOT 5
- Should I define severices for each main package HOT 4
- Memory leak when definition unshared true HOT 2
- Request Scope Isolation question
- Tags on v2 HOT 2
- Authentication middleware example HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from di.