0

I want to write a Unit test that will test to see if all DI services can/will be resolved at runtime.

We have 2 .net core web apps. App A and App B, and a shared BLL DLL that both web apps reference that we add services to.

When we create a new service in the BLL project for the web app we are currently working on (let’s say App A) we will include the Service in our Startup > ConfigureServices method and register it with the DI container as we should but may forget to do the same with App B only to realize at runtime that dependency resolution will fail for it.

How can we protect against this?

5
  • Create a shared library where the registration can be done in one place as an extension method. That once added it should be available to dependent. Trying to unit test di is the wrong approach.
    – Nkosi
    Oct 8, 2019 at 1:26
  • Since bll is shared reference that move registration there. There are multiple approaches that can be used.
    – Nkosi
    Oct 8, 2019 at 1:30
  • While I believe I understand what you are suggesting, the question still remains. Lets say there is no project App B or even a BLL DLL. We create a service and forget to register it (all in the same app - App A). We would not know this with a unit test that mocks the service and would still only come to understand it is not registered until runtime. What then?
    – AJ Soto
    Oct 8, 2019 at 2:13
  • But what if you forget to write the unit test to check the new service is registered? You still wouldn't know until runtime. It's the same as forgetting to populate a string and not checking for null. You need to be vigilant and aware of what you need to do when creating a new service.
    – Simply Ged
    Oct 8, 2019 at 4:59
  • While I understand what you’re saying. You could write a test to make sure a string isn’t null, but more than that, being vigilant can be said about writing code and not needing tests at all. Maybe my question is poorly worded. But just like auto mapper let’s you test mapping configurations to prevent runtimes errors if you modify the properties of a object being mapped. I wonder if the same can be done in some way for DI or perhaps any practices to avoid these issues. I just want to know if something exists I’m not aware of that may help. Right now it looks like integration tests.
    – AJ Soto
    Oct 8, 2019 at 13:35

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy