I start working in Unit Testing, Dependancy Injection and all this jazz, creating my latest ASP.NET MVC project.
Now I want to want Unit Test my controllers, and it's hard for me to figure out how to do this without an IoC container.
Take, for example, a simple controller:
public class QuestionsController : ControllerBase { private IQuestionsRepository _repository = new SqlQuestionsRepository();
This class is not a fully testable module because of its direct creation by SqlQuestionsRepository. So, let's go down the injection route and do the following:
public class QuestionsController : ControllerBase { private IQuestionsRepository _repository; public QuestionsController(IQuestionsRepository repository) { _repository = repository; } }
It seems better. Now I can easily write unit tests with mock IQuestionsRepository. However, what will now create the controller instance? Somewhere further down the SqlQuestionRepository call chain, an instance should be created. It seems that I just shifted the problem elsewhere, did not get rid of it.
Now I know that this is a good example of where the IoC container can help you by connecting the controller requirements for me, while at the same time so that my controller can easily test the unit.
My question is, how can unit testing be done on such things without an IoC container?
Note. I am not opposed to IoC containers, and I will likely go down this road soon. However, I am curious that there is an alternative for people who do not use them.
unit-testing asp.net-mvc inversion-of-control ioc-container controller
anon
source share