6

私は簡単なのHttpApplicationクラスを持っている:MVC 3のエリア登録ロジックをテストするにはどうすればよいですか?

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

     // register other stuff... 
    } 
} 

私のユニットテストは、ApplicationStartを起動し、アプリケーション起動時の動作を確認し、HttpApplicationを初期化します。

このアプローチは、MVC領域を統合するまでうまく機能しました。 AreaRegistration.RegisterAllAreas()はユニットテストによって呼び出されると、次の例外がスローされます:

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

試験領域の初期化ロジックのための良い方法はありますか?

答えて

4

一時的な回避策:

MvcApplicationで

1)、露出仮想メソッドRegisterAllAreas()

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

     // register other stuff... 
    } 

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

2)明細書では、プロキシを実装:

[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)テストAreaRegistrationの実装個別

4)は、私は、このアプローチが好きではありませんが、今はよりよい解決策を考えることはできませんテストカバレッジ

からMvcApplication.RegisterAllAreas()を除外します。
アイデアとコメントは大歓迎です…

関連する問題