asp.net-mvc – 如何使用Moq测试一个自定义的ModelBinder?
发布时间:2020-12-15 23:14:19 所属栏目:asp.Net 来源:网络整理
导读:我在编写一些单元测试时遇到困难,以测试我创建的自定义ModelBinder.我试图单元测试的ModelBinder是我发布了 here的JsonDictionaryModelBinder. 我遇到的问题是使用Moq获取所有的Mocking设置.由于HttpContextBase没有被正确地模拟,所以我不断得到空的异常.我
我在编写一些单元测试时遇到困难,以测试我创建的自定义ModelBinder.我试图单元测试的ModelBinder是我发布了
here的JsonDictionaryModelBinder.
我遇到的问题是使用Moq获取所有的Mocking设置.由于HttpContextBase没有被正确地模拟,所以我不断得到空的异常.我认为. 有人能帮我弄清楚我没有做什么相关的事情吗? 以下是单元测试的示例我正在尝试写入不起作用: [TestMethod()] public void BindModelTest() { JsonDictionaryModelBinder target = new JsonDictionaryModelBinder(); NameValueCollection nameValueCollection = new NameValueCollection() { {"First","1"},{"Second","2"},{"Name","Chris"},{"jsonValues","{id: 200,name: 'Chris'}"} }; HttpContextBase httpContext = MockHelper.FakeHttpContext(HttpVerbs.Post,nameValueCollection); ControllerContext controllerContext = new ControllerContext(new RequestContext(httpContext,new RouteData()),new Mock<Controller>().Object); Predicate<string> predicate = propertyName => (propertyName == "jsonValues"); ModelBindingContext bindingContext = new ModelBindingContext() { Model = null,ModelType = typeof(JsonDictionary),ModelState = new ModelStateDictionary(),PropertyFilter = predicate,ValueProvider = new Dictionary<string,ValueProviderResult>() { { "foo",null } } }; //object expected = null; // TODO: Initialize to an appropriate value var actual = target.BindModel(controllerContext,bindingContext) as JsonDictionary; Assert.IsNotNull(actual); Assert.AreEqual("Chris",actual["name"]); //Assert.AreEqual(expected,actual); Assert.Inconclusive("Verify the correctness of this test method."); } 这是上面使用的“FakeHttpContext”方法: public static class MockHelper { public static HttpContextBase FakeHttpContext(HttpVerbs verbs,NameValueCollection nameValueCollection) { var httpContext = new Mock<HttpContextBase>(); var request = new Mock<HttpRequestBase>(); request.Setup(c => c.Form).Returns(nameValueCollection); request.Setup(c => c.QueryString).Returns(nameValueCollection); var response = new Mock<HttpResponseBase>(); var session = new Mock<HttpSessionStateBase>(); var server = new Mock<HttpServerUtilityBase>(); httpContext.Setup(c => c.Request).Returns(request.Object); var u = verbs.ToString().ToUpper(); httpContext.Setup(c => c.Request.RequestType).Returns( verbs.ToString().ToUpper() ); httpContext.Setup(c => c.Response).Returns(response.Object); httpContext.Setup(c => c.Server).Returns(server.Object); httpContext.Setup(c => c.User.Identity.Name).Returns("testclient"); return httpContext.Object; } } 解决方法
罪魁祸首是这一行:
httpContext.Setup(c => c.Request.RequestType).Returns( verbs.ToString().ToUpper() ); 这在技术上是Request对象上的第二个安装程序,它正在擦除原始的安装程序,即使您要在对象层次结构中“过去”.我不知道这是否是Moq或所需行为的错误,我以前也遇到过,并没有得到检查. 您可以通过将该行移动到上面设置您的请求并直接设置,而不是通过httpContext来解决.所以, request.Setup(c => c.RequestType).Returns(verbs.ToString().ToUpper()); 我也注意到你声明的“var u”没有被使用;) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc-4 – 在EF迁移配置类的Seed方法中获取App_Data
- 在ASP.NET中上传文件时运行病毒扫描
- ASP.NET Core根据环境切换NLog配置
- asp.net – 开发和测试环境最佳实践?
- asp.net-mvc – 如何在版本信息中包含静态内容
- asp.net-mvc-2 – 我的MVC2应用程序可以在查询字符串参数上
- ASP.NET CSS CSS语法问题中的类与CSSClass的区别
- asp.net – 检查ValidationGroup是否从代码隐藏有效
- 从ASP.NET MVC返回null时,jQuery post JSON失败
- asp.net-mvc – MVC最后一次在向用户呈现之前更改响应的机会
推荐文章
站长推荐
- 经典ASP亚马逊s3休闲授权
- asp.net – “可能危险的Request.Form”在通用处
- ASP.NET 5中的手动防伪标记创建和验证
- asp.net-mvc-3 – mvc是否支持整个区域的Web.con
- asp.net-mvc – 在视图中创建DropDownListFor项
- asp.net – DropDownList OnSelectedIndexChange
- asp.net-mvc – Url.RouteUrl返回null
- asp.net-ajax – system.web不包含脚本的定义
- asp.net – asp mvc“一个有潜在危险的Request.F
- asp.net – “无法在Web服务器上启动调试”MSVSM
热点阅读