Will the IOC be able to solve our problems?

Just trying to inject unit testing into a system like brownfield. Keep in mind that I'm relatively new to the world of unit testing. Of course, this will be a gradual migration, because there are so many areas of pain.

The current problem I'm trying to solve is that we followed a lot of bad practices from our VB6 days and converting our application to .Net. We have many common / static functions that call other common functions, and others call others, etc. Sometimes defendations are passed as parameters, and sometimes they are just created in the calling function. I have already instructed our developers to stop creating common functions and instead create member instances and use only those member members from the interfaces, but this does not ease the current situation. Thus, you must recursively go through each dependency at the top level for each function of your code, and the method signatures turn into a mess.

I hope this is what the IOC will fix. We are currently using NUnit / Moq, and I am starting to explore StructureMap. So far, I understand that you pretty much tell StructureMap for the x interface. I want to use the specific y class by default:

ObjectFactory.Initialize(x=>{x.ForRequestType<IInterface>().TheDefaultIsConcreteType<MyClass>()}); 

Then at runtime:

 var mytype = ObjectFactory.GetInstance<IInterface>(); 

IOC container initializes the correct type for you. Not sure how to change the fake to a specific type, but hopefully it's simple. Again, the IOC will solve the problems that I mentioned above? Is there a specific IOC structure that will do this better than StructureMap, or whether they can handle this situation. Any help would be greatly appreciated.

+4
source share
2 answers

To a large extent, this is not a silver bullet.

Note that the IOC container must be installed in the root directory of your application. Not ad-hoc. Otherwise, you will implement a service locator, which is an anti-pattern.

Follow the design input for your production code, allow the IOC container to resolve your dependencies. For unit tests, you simply code your dependencies. This will allow you to use mock objects (test doubles). In other words, IOC has nothing to do with unit testing.

+4
source

IoC is great for getting rid of static methods and preventing dependency delegation, because it is easy for each class to explicitly declare all dependencies.

As for unit testing, there are two camps:

  • Insert Mocks as dependency instances in the IOC container that you do not want to test in a specific case, and allow the container as usual. This works especially well if you want to test multiple classes as an integrated component.
  • Check all the instances you need to create one class object for validation and insert them manually. This works especially well if you want to stick to testing each class separately (your unit is one class).
+1
source

Source: https://habr.com/ru/post/1303986/


All Articles