How to check zone registration logic in MVC 3?

I have a simple HttpApplication class:

public class MvcApplication : HttpApplication { public void Application_Start() { // register areas AreaRegistration.RegisterAllAreas(); // register other stuff... } } 

My module tests the initialization of HttpApplication , calls ApplicationStart and checks the application startup behavior.

This approach worked well until I had to integrate MVC domains. When AreaRegistration.RegisterAllAreas() is called unit test, the following exception is thrown:

System.InvalidOperationException: This method cannot be called during the application pre-start initialization stage.

Is there a good approach for testing area initialization logic?

+6
source share
1 answer

Temporary workaround:

1) In MvcApplication, output the RegisterAllAreas() virtual method

 public class MvcApplication : HttpApplication { public void Application_Start() { // register areas RegisterAllAreas(); // register other stuff... } public virtual void RegisterAllAreas() { AreaRegistration.RegisterAllAreas(); } } 

2) In the specification, execute the proxy:

 [Subject(typeof(MvcApplication))] public class when_application_starts : mvc_application_spec { protected static MvcApplication application; protected static bool areas_registered; Establish context = () => application = new MvcApplicationProxy(); Because of = () => application.Application_Start(); It should_register_mvc_areas = () => areas_registered.ShouldBeTrue(); class MvcApplicationProxy : MvcApplication { protected override void RegisterAllAreas() { areas_registered = true; } } } 

3) Testing AreaRegistration implementations individually

4) Exclude MvcApplication.RegisterAllAreas() from the test coverage

I do not like this approach, but we cannot think of a better solution right now.
Ideas and comments are welcome & hellip;

+4
source

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


All Articles