How can I get test coverage around AreaRegistration.RegisterAllAreas in ASP.NET MVC?

So, I shoot 100% coverage on the MVC3 site, and we use areas. I can cover everything else, EXCEPT for this one line in Application_Start :

 AreaRegistration.RegisterAllAreas(); 

I have already thoroughly tested the registration of each area, so it really comes down to an integration test, but I still would like to somehow cover it up without resorting to the CoverageExclude attribute or reducing the percentage of coverage.

Note that the module testing this on NUnit explodes with the following exception fragment:

 System.InvalidOperationException : This method cannot be called during the application pre-start initialization stage. at System.Web.Compilation.BuildManager.EnsureTopLevelFilesCompiled() at System.Web.Compilation.BuildManager.GetReferencedAssemblies() 

Any ideas?

+4
source share
2 answers

I feel that trying to target a 100% test coverage of an application is not always the best approach. Sure that your application should be well tested, but push everything aside to make sure that you have 100% coverage, you focus on the quality of the tests and more about coverage. Coverage is just a rough guide and does not tell you if your tests really check what exactly should be, just called "line x".

A high TC level is good, but I would say if at 100% you will not test correctly. I personally would not check this.

+4
source

Extract code that populates routes to a method that takes a RouteCollection as a parameter. Then you can call it in your test by passing an empty collection and make sure that it contains all the necessary routes.

  public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( "Default_without_optional_params", // Route name "{controller}.aspx/{action}", // URL with parameters new { controller = "Home", action = "Index" } // Parameter defaults ); } protected void Application_Start() { RegisterRoutes(RouteTable.Routes); } 

The same applies to areas. Extract the method that takes the AreaRegistrationContext .

+2
source

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


All Articles