我怎么会嘲笑一个查询字符串(how would I mock a querystring)

2019-07-30 16:29发布

使用起订量以下

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

    }

}

干杯!

Answer 1:

你可以嘲笑与索引SetupGet方法:

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


文章来源: how would I mock a querystring