开发者

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

So I'm shooting for 100% coverage on an MVC3 site, and we're using areas. I can get cover开发者_高级运维age on everything else EXCEPT for this one line in Application_Start:

AreaRegistration.RegisterAllAreas();

I've already thoroughly tested each area's registration, so this really amounts to an integration test of sorts, but I'd still like to cover this somehow without having to resort to a CoverageExclude attribute or lowering the coverage percentage.

Note that unit testing this in NUnit this explodes with the following exception snippet:

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

Any ideas?


I feel trying to aim for 100% test coverage on an application is not always the best approach. Sure your app should be well tested but pushing everything aside to make sure you have 100% coverage stops you focusing on the quality of the tests and more about the coverage. Coverage is just a rough guide and does not tell you if your tests are actually testing what they should be only that "line x" got called.

Having high TC is good but I'd say if there's 100% you're not testing properly. personally I wouldn't test this.


Extract the code that populates routes to a method that takes RouteCollection as a parameter. You can then call it in your test passing an empty collection and verify that it contains all the routes you need.

 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 a method that takes AreaRegistrationContext.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜