asp.net-mvc – 如何使用Moq测试一个自定义的ModelBinder?
发布时间:2020-05-24 03:34:03 所属栏目: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 – 如何通过使用eval应用特定FORMAT的日期?
- ASP.NET云应用程序与普通的ASP.NET
- 如果复选框被选中,ASP.NET – 需要一个文本框
- 如何在ASP.NET自定义控件中持久保存List属性?
- 没有完整PostBacks的ASP.NET JavaScript回调?
- asp.net – 为什么HttpUtility.UrlPathEncode标记为“不使用
- asp.net-core – 什么应该是WEB API Action Method的返回类
- asp.net-mvc – 应该在ASP.NET MVC应用程序中缓存发生在哪里
- asp.net-mvc – 如何使用Visual Studio 2013和Entity Frame
- asp.net-mvc-4 – 在控制器的新窗口中打开mvc视图
推荐文章
站长推荐
- ASP.NET Ajax库死了吗?
- asp.net-mvc – VIEWDATA和VIEWBAG存储在MVC中的
- asp.net-core-mvc – 什么是Asp.Net Core MVC中的
- asp-classic – 如何从此ADODB.Recordset获取插入
- asp.net-mvc – 确定哪个控制器和操作在ASP.NET
- asp.net – MVC3何时使用区域?
- asp.net-mvc-3 – Orchard CMS DataAnnotations
- asp.net-mvc – 在MVC 3中回发到控制器操作后,Vi
- asp.net – 使用OAuth在Web API中进行身份验证
- asp.net – OutputCache和RenderAction缓存整个页
热点阅读
