Dependency Injection: Turtles all the way down?

后端 未结 5 863
执笔经年
执笔经年 2020-12-01 18:11

So I\'m wondering about how unit testing works in regards to dealing external dependencies. Here and elsewhere I\'ve become familiar with dependency injection, and how that

5条回答
  •  不知归路
    2020-12-01 18:56

    When you unit test a class, you should mock its dependencies, to test your class in isolation -- this is regardless of dependency injection.

    The answer to your question about Bar is: yes, you should inject Foo. Once you go down the DI path, you will use it across your entire stack. If you really need a new Foo for every doSomethingWithFoo call, you might want to inject a FooFactory (which you can then mock for testing purposes), if you want a single Bar to use many Foos.

提交回复
热议问题