mercredi 14 juillet 2021

How to solve hierarchy and complex dependency injections when unit testing?

I need to unit test a method which resides in the service. Let's call this service as a mainService. MainService depends on OneService, then OneService depends on 10 other services. It is happening a lot like this in a hierarchical manner. I was reading about Dependency Injection and thinking how to make it simpler when especially doing unit testing.

TwoService = MockRepository.GenerateStub<ITwoService>();
ThreeService = MockRepository.GenerateStub<IThreeervice>();
FourService = MockRepository.GenerateStub<IFourService>();
//...and more
OneService oneService = new IOneService(TwoService, ThreeService, FourService, FiveService, SixService, and more);

MainService obj = new MainService(oneService);

As you see from the example, it's just one or two hierarchy but in real situations, it's more complex and have 3 or 4 hierarchy dependency injected. How do we solve this problem by simplifying the design or make it simpler for unit testing. Unit testing is a good example to test dependency injection. That's why I am continuously talking about in unit testing.

Aucun commentaire:

Enregistrer un commentaire