5

私が書いたカスタム ビュー エンジンの単体テストを試みています。

ビュー エンジンの意図した機能は、FindView の実行時にベース RazorViewEngine が見える場所を変更することです。

これが私の単体テストです

public void ViewEngineReturnsDependencyView()
{
    //Mock http request
    var mockRequest = new Mock<HttpRequestBase>();
    //Mock server variable
    NameValueCollection variables = new NameValueCollection();
    variables.Add("APPL_PHYSICAL_PATH", TEST_APPLICATION_PATH);
    mockRequest.Setup(r => r.ServerVariables).Returns(variables);

    //Mock http context
    var mockHttpContext = new Mock<HttpContextBase>();

    //Mock route
    mockHttpContext.Setup(c => c.Request).Returns(mockRequest.Object);
    var routeData = new RouteData();
    routeData.Values.Add("controller", "testController");
    routeData.Values.Add("action", "testAction");

    //Mock controller context
    var controllerContext = new testController().ControllerContext;
    controllerContext.HttpContext = mockHttpContext.Object;
    controllerContext.RouteData = routeData;
    var mockControllerContext = new ControllerContext(mockHttpContext.Object,
                        routeData, 
                        new Mock<ControllerBase>().Object);

    //Run find view
    viewEngine.FindView(mockControllerContext, "TestView", null, false);
}

迷惑なviewEngine.FindView(...);ことに例外をスローします:

テスト メソッド ... 例外がスローされました: System.NullReferenceException: オブジェクト参照がオブジェクトのインスタンスに設定されていません。結果のスタック トレース:

System.Web.WebPages.DisplayModeProvider.GetDisplayMode (HttpContextBase コンテキスト) で

System.Web.Mvc.VirtualPathProviderViewEngine.GetPathFromGeneralName (ControllerContext controllerContext、List`1 の場所、文字列名、文字列 controllerName、文字列 areaName、文字列 cacheKey、文字列[]&searchedLocations) で

System.Web.Mvc.VirtualPathProviderViewEngine.GetPath (ControllerContext controllerContext、文字列 [] の場所、文字列 [] areaLocations、文字列の場所PropertyName、文字列の名前、文字列の controllerName、文字列の cacheKeyPrefix、ブール値の useCache、文字列 [] &searchedLocations) で

System.Web.Mvc.VirtualPathProviderViewEngine.FindView (ControllerContext controllerContext、文字列 viewName、文字列 masterName、ブール値の useCache) で

...\Mvc\CustomRazorViewEngine.cs:line 85 の ...Mvc.CustomRazorViewEngine.FindView(ControllerContext controllerContext, String viewName, String masterName, Boolean useCache) で

...Tests.MVC.ViewEngine.ViewEngineReturnsDependencyView() で ...Tests\MVC\ViewEngine.cs: 78 行目

私の質問は、RazorViewEngine.FindView() を単体テストするための適切なモックを作成するにはどうすればよいですか?

4

2 に答える 2