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.
source share