When trying to stick to D in SOLID
The principle of dependency inversion, where "Depend on abstractions" is needed. Independent of nodules
for the project. In this case, Asp.Net-MVC5, I needed a way to make sure that all the controllers (MVC and WebAPI2) follow this pattern, where they are independent of nodules.
The initial idea was taken from an article I read, where a unit test was created to scan all the controllers to make sure they have explicit permission. I applied similar thinking in verifying that all controllers have abstraction-dependent constructors.
[TestClass] public class ControllerDependencyTests : ControllerUnitTests { [TestMethod] public void All_Controllers_Should_Depend_Upon_Abstractions() { var controllers = UnitTestHelper.GetAssemblySources()
So, let's give the following example. (Note: I adapted this for MVC)
public class MovementController : Controller { public MovementController(MotorController motorController) {
unit test will fail ...
Result Message: Assert.Fail failed. Type depends on concretion instead of its abstraction. 1 Found : MovementController(MotorController motorController)
This worked for me because I had a generic type that needs to be searched with IController and ApiController .
There is room for improvement in the test, but this should be a good starting point for you.
Nkosi source share