asp.net-mvc-4 - 使用 Kendo UI 对 MVC4 中的 Controller 操作进行单元测试

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

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

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);

}

Controller 上下文是用以下代码伪造的:

 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 Controller 操作尝试调用 ToDataSourceResult 方法时,会发生异常。

System.EntryPointNotFoundException : Entry point was not found.

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

最佳答案

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 文件中添加相同的运行时绑定(bind)信息来修复您的测试项目:

<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>

希望对您有所帮助。

关于asp.net-mvc-4 - 使用 Kendo UI 对 MVC4 中的 Controller 操作进行单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17188730/

相关文章:

css - 可以在弹出窗口中使用@media 查询吗?

kendo-ui - Kendo Tabstrip 中的预加载

asp.net-mvc - 减小 asp.net mvc Controller 的大小

c# - ASP.Net MVC4 beta - HttpClient 中没有await 关键字支持?

asp.net-mvc-4 - MVC 4 如何有条件地添加 <div> 元素

javascript - 设置 Kendo 网格过滤器单元格值

asp.net-mvc - ASP.NET MVC 4 区域的 Ajax 请求问题

jquery - Kendo UI 下拉列表在更改边框颜色时丢失数据

validation - 使用 Kendo UI 在 ASP MVC 中进行必填字段验证

kendo-asp.net-mvc - Kendo UI MVC - 在 HtmlAttributes 中不显示任何内容