我将如何模拟查询字符串 [英] how would I mock a querystring
本文介绍了我将如何模拟查询字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Moq中使用以下内容
using the following in Moq
public Mock<HttpRequestBase> Request { get; set; }
我如何嘲笑此请求[....]
how can I mock this Request[....]
(in controller)
var modelFromPost = Request["mymodel"]
这是我到目前为止所拥有的
here's what I have so far
public class ContextMocks
{
public Mock<HttpContextBase> HttpContext { get; set; }
public Mock<HttpRequestBase> Request { get; set; }
public RouteData RouteData { get; set; }
public ContextMocks(Controller controller)
{
HttpContext = new Mock<HttpContextBase>();
HttpContext.Setup(x => x.Request).Returns(Request.Object);
}
}
欢呼!
推荐答案
您可以使用SetupGet
方法模拟索引器:
You can mock indexers with the SetupGet
method:
ContextMocks.Request.SetupGet(r => r["mymodel"]).Returns(myModel);
这篇关于我将如何模拟查询字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文