7

在起订量中使用以下内容

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

    }

}

干杯!

4

1 回答 1

6

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

ContextMocks.Request.SetupGet(r => r["mymodel"]).Returns(myModel);
于 2012-07-31T06:34:53.887 回答