我将如何模拟查询字符串

时间:2012-07-31 06:14:11

标签: asp.net-mvc-3 mocking

在Moq中使用以下内容

public Mock<HttpRequestBase> Request { get; set; }

如何模拟此请求[....]

(in controller)
var modelFromPost = Request["mymodel"]

这是我到目前为止所拥有的

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

    }

}

喝彩!

1 个答案:

答案 0 :(得分:6)

您可以使用SetupGet方法模拟索引器:

ContextMocks.Request.SetupGet(r => r["mymodel"]).Returns(myModel);