【问题标题】:Unit Testing Controller Actions in MVC4 with Kendo UI使用 Kendo UI 在 MVC4 中单元测试控制器操作
【发布时间】:2013-06-15 20:08:51
【问题描述】:

我正在为我们的控制器编写一些单元测试。我们有以下简单的控制器。

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 内部字典在单元测试中为空(而不是在标准上下文中运行时)。如果从ToDataSourceResult 方法中删除ModelState,则测试成功通过。非常感谢任何帮助。

【问题讨论】:

    标签: asp.net-mvc-4 kendo-ui kendo-asp.net-mvc


    【解决方案1】:

    JustDecompile 的快速峰值显示 Kendo.Web.Mvc.dll 是针对 System.Web.Mvc 3.0 版构建的。您的测试项目可能引用了较新版本的 ASP.NET MVC (4.0),因此在运行时对System.Web.Mvc 成员的任何调用都会导致System.EntryPointNotFoundException,因为这些成员无法解析。在您的特定情况下,对 KendoUI MVC 扩展方法 ToDataSourceResult() 的调用以及随后对 ModelState.IsValid 的调用是罪魁祸首。

    这一切都在您的应用程序中正常运行的原因是,您的项目默认配置为 redirect assembly bindings,作为 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>
    

    希望对你有帮助。

    【讨论】:

    • 非常感谢......我自己永远不会得出这个结论。
    • 这个答案拯救了我的一天!
    猜你喜欢
    • 1970-01-01
    • 2014-01-16
    • 1970-01-01
    • 2020-09-05
    • 1970-01-01
    • 1970-01-01
    • 2016-01-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多