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);
}
}
cheers!
Request. QueryString. Count != 0 will simply tell you if there are no parameters at all.
To pass in parameter values, simply append them to the query string at the end of the base URL. In the above example, the view parameter script name is viewParameter1.
The QueryString collection is used to retrieve the variable values in the HTTP query string. The line above generates a variable named txt with the value "this is a query string test". Query strings are also generated by form submission, or by a user typing a query into the address bar of the browser.
You can mock indexers with the SetupGet
method:
ContextMocks.Request.SetupGet(r => r["mymodel"]).Returns(myModel);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With