6

Tôi có lớp HttpApplication đơn giản:Làm cách nào để kiểm tra logic đăng ký khu vực trong MVC 3?

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

     // register other stuff... 
    } 
} 

kiểm tra đơn vị của tôi khởi HttpApplication, gọi ApplicationStart và xác minh ứng dụng hành vi bắt đầu-up.

Cách tiếp cận này hoạt động tốt cho đến khi tôi phải tích hợp các khu vực MVC. Khi AreaRegistration.RegisterAllAreas() được gọi bằng một thử nghiệm đơn vị, các ngoại lệ sau đây được ném:

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

Có một cách tiếp cận tốt cho logic khởi động khu vực thử nghiệm?

Trả lời

4

workaround tạm thời:

1) Trong MvcApplication, tiếp xúc với phương pháp ảo RegisterAllAreas()

public class MvcApplication : HttpApplication 
{ 
    public void Application_Start() 
    { 
     // register areas 
     RegisterAllAreas(); 

     // register other stuff... 
    } 

    public virtual void RegisterAllAreas() 
    { 
     AreaRegistration.RegisterAllAreas(); 
    } 
} 

2) Trong đặc điểm kỹ thuật, thực hiện một 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) Kiểm tra AreaRegistration triển khai riêng

4) Loại trừ MvcApplication.RegisterAllAreas() từ bảo hiểm thử nghiệm

tôi không thích cách tiếp cận này, nhưng không thể nghĩ ra một giải pháp tốt hơn ngay bây giờ.
Ý tưởng và nhận xét được chào đón & hellip;

Các vấn đề liên quan