Unit Test MVC with ASP.NET Dev Server

I want to confirm that my HomeController class is selected on the route that I created. So I have a test like this:

[TestMethod] [UrlToTest("http://localhost:14478/home")] [HostType("ASP.NET")] [AspNetDevelopmentServerHost("$(SolutionDir)\\MvcBuildApp")] public void MyTest() { System.Diagnostics.Debugger.Break(); RouteCollection routes = new RouteCollection(); MvcApplication.RegisterRoutes(routes); MvcApplication.RegisterGlobalFilters(GlobalFilters.Filters); //This fetches DefaultControllerFactory (for now, anyway...) var factory = ControllerBuilder.Current.GetControllerFactory(); //mock a context... var httpContext = CreateHttpContext("http://localhost:14478/home", "GET"); RouteData route = routes.GetRouteData(httpContext); var ctrlInstance = factory.CreateController(new RequestContext(httpContext, route), (string)route.Values["controller"]); //ASSERT the instance is of type "HomeController" //... } 

It doesnโ€™t work, saying that 'http://localhost:14478/home' completed successfully without running the test.

I noticed that in the VS output window there is also the message No connection could be made because the target machine actively refused it 127.0.0.1:14478 . I thought Cassini should not be active. Therefore, I decided to run the test site (ctrl + F5) before running unit test. He then changed the output of VS to this:

WebTestAdapter.ConnectToHostAdapter: An unexpected exception occurred. Microsoft.VisualStudio.TestTools.HostAdapters.AbortTestExecutionException: Application Error. at Microsoft.VisualStudio.TestTools.HostAdapters.WebTestAdapter.ConnectToHostAdapter ()

To try to resolve this, I followed the tips of these articles:

... but I still get the error, no matter what I do. Suggestions?

UPDATE / EXPLANATION
This question is not about testing MVC routes. The purpose of this question is to learn how to make ASP.NET MVC properly initialized to allow more "in-depth" automated testing. I chose the โ€œtest routeโ€ scenario, instead of DefaultControllerFactory, as an example. In this example, the DefaultControllerFactory does not work properly if ASP.NET MVC is not initialized correctly.

+6
source share
2 answers

You might be interested in looking at Steve Sanderson's MVC testing framework. If you download the source, you will have a great example of how to initialize the MVC framework:

http://blog.codeville.net/2009/06/11/integration-testing-your-aspnet-mvc-application/

0
source

This is a common requirement when testing MVC applications, and you are lucky to have a structure that will simplify your life when testing MVC controllers:

Instruments:

http://mvccontrib.codeplex.com/

And as an example, you can create tests with only one line:

 "~/".ShouldMapTo<HomeController>(controller => controller.Index()); 

As a reference:

http://geekswithblogs.net/thomasweller/archive/2009/11/02/unit-testing-asp.net-mvc-routes.aspx

http://www.arrangeactassert.com/how-to-unit-test-asp-net-mvc-controllers/

+1
source

Source: https://habr.com/ru/post/915133/


All Articles