0

in unit tests for init and create the objects I would like to use buit-in Microsoft DI, is it good practice?

1
  • 2
    The point of DI is so your tests can mock those dependencies. Commented Jun 21 at 5:53

1 Answer 1

1

There are many different types of tests and even for "unit tests" answer depends on your definition of "unit test" and what you are actually testing. Answer also depends on approaches you have in your team, on libraries you use, some implementation details and etc.

In general I would say

  • if DI in some particular case makes your code more readable, smaller (questionable) and clean, then use DI
  • If you want to test some piece of code which is far away from DI (questions like "what is it exactly" and "should\shouldn't you do that" are out of scope) and DI usage here does not bring any benefit, then it is fine to create object manually

Not the answer you're looking for? Browse other questions tagged or ask your own question.