Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mocking an IoC Container?

Does it make sense to mock an IoC container? If so how would I go about it, using Moq?

I am creating a Prism 4 app, using Unity 2.0 as the IoC container. I inject the container into classes that need its services, rather than using Prism's ServiceLocator. For unit testing, unless I need other Prism services for my test, I simply instantiate the container and register mocks with it. I pass the container to the class under test, which resolves the mocks.

It's all fairly simple, but I am wondering if I should mock the container? Why? If so, how would I do that, if I am using Moq as my mocking framework? Thanks for your help.

like image 514
David Veeneman Avatar asked Apr 20 '26 22:04

David Veeneman


1 Answers

No, it doesn't make sense to mock a DI container because application classes should not reference a container at all.

Instead of injecting the container into the classes, you should inject only the services that they need. This will also mean that you can unit test them without referencing a DI container at all.

like image 103
Mark Seemann Avatar answered Apr 23 '26 22:04

Mark Seemann



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!