6

コントローラーの単体テストを作成中です。次の単純なコントローラーがあります。

public class ClientController : Controller
{

    [HttpPost]
    public ActionResult Create(Client client, [DataSourceRequest] DataSourceRequest request)
    {
        if (ModelState.IsValid)
        {
            clientRepo.InsertClient(client);
        }

        return Json(new[] {client}.ToDataSourceResult(request, ModelState));
    }
}

この単体テストは次のとおりです。

[Test]
public void Create()
{
        // Arrange
        clientController.ModelState.Clear();

        // Act
        JsonResult json = clientController.Create(this.clientDto, this.dataSourceRequest) as JsonResult;

        // Assert
        Assert.IsNotNull(json);

}

また、コントローラー コンテキストは次のコードで偽造されます。

 public class FakeControllerContext : ControllerContext
    {
        HttpContextBase context = new FakeHttpContext();

        public override HttpContextBase HttpContext
        {
            get
            {
                return context;
            }
            set
            {
                context = value;
            }
        }

    }

    public class FakeHttpContext : HttpContextBase
    {
        public HttpRequestBase request = new FakeHttpRequest();
        public HttpResponseBase response = new FakeHttpResponse();

        public override HttpRequestBase Request
        {
            get { return request; }
        }

        public override HttpResponseBase Response
        {
            get { return response; }
        }
    }

    public class FakeHttpRequest : HttpRequestBase
    {

    }

    public class FakeHttpResponse : HttpResponseBase
    {

    }


}

コントローラー アクションがメソッドCreateを呼び出そうとすると、例外が発生します。ToDataSourceResult

System.EntryPointNotFoundException : Entry point was not found.

デバッグは、単体テストで ModelState 内部ディクショナリが空であることを示しています (標準コンテキストで実行した場合ではありません)。ModelStateがメソッドから削除された場合ToDataSourceResult、テストは成功します。どんな助けでも大歓迎です。

4

1 に答える 1

5

JustDecompileのクイック ピークにより、 Kendo.Web.Mvc.dllがSystem.Web.Mvcバージョン 3.0に対してビルドされていることがわかります。テスト プロジェクトはおそらく新しいバージョンの ASP.NET MVC (4.0) を参照しているため、実行時にメンバーを呼び出すと、これらのメンバーを解決できないため、結果が になります。あなたの特定のケースでは、KendoUI MVC 拡張メソッドの呼び出しとそれに続く呼び出しが原因でした。System.Web.MvcSystem.EntryPointNotFoundExceptionToDataSourceResult()ModelState.IsValid

これがすべてアプリケーションでエラーなしで機能する理由は、Visual Studio ASP.NET MVC プロジェクト テンプレートの一部として、ランタイムが ASP.NET MVC の最新バージョンを対象とするようにアセンブリ バインディングをリダイレクトするようにプロジェクトが既定で構成されているためです。組み立てた。App.config ファイルに同じランタイム バインディング情報を追加することで、テスト プロジェクトを修正できます。

<configuration>
    <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
            <dependentAssembly>
                <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
                <bindingRedirect oldVersion="1.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
            </dependentAssembly>
        </assemblyBinding>
    </runtime>
</configuration>

それが役立つことを願っています。

于 2013-07-01T19:55:10.037 に答える