asp.net mvc - How are you able to Unit Test your controllers without an IoC container? -


I started entering unit testing, dependency injection and all jazz while creating my latest ASP.NET MVC project I am

I am at that point where I would like to test the unit of my controllers and I am having difficulty finding out how to do it properly without the IoC container.

Example A simple controller:

  public class question controller: controlbase {private IQuestionsRepository _repository = new SqlQuestionsRepository (); // ... continue with different controller functions}  

This class is not a very unit test, because SQL queries are down from the dependency injection route due to its quick launch of the repository Do this by visiting:

  public class question controller: controlbase {Private IQuestionsRepository _repository; Public Query Controller (IQuestionsRepository repository) {_repository = repository; }}  

It looks better now I can easily write unit tests with a fake IQuestionsRepository, however, is going to instantize the controller now? The SqlQuestionRepository will have to be done immediately in the call series. It seems that I have just moved this problem elsewhere, it does not get rid of it.

Now, I know that this is a good example where an IOC container allows you to be cordless for controllers while keeping my controller easily unit-tested at the same time

My question is, how can one estimate the unit testing on things of this nature without any IoC container without ?

Note: I do not oppose the IoC container, and I can go under that road soon. However, I am curious that alternative people are for those people who do not use them.

Is it possible to maintain direct flow of the area and provide a setter? In this case, you will only be calling the Setter during unit testing. Something like this:

  public class question controller: controlbase {private IQuestionsRepository _repository = new SqlQuestionsRepository (); // In fact, only during unit testing is said ... Public Query Controller (IQuestionsRepository repository) {_repository = repository; }}  

I am not familiar with the Net, but as a side note in Java this is an existing method which is a common way of refining existing code to improve the testability. . IE, if you have a class already in use and need to be modified so that code coverage can be improved without breaking the existing activity.

Our team has done this before, and usually we keep the setter's visibility package-private and the package of test class in order to make it call the setter.


Comments

Popular posts from this blog

c++ - Linux and clipboard -

What is expire header and how to achive them in ASP.NET and PHP? -

sql server - How can I determine which of my SQL 2005 statistics are unused? -