Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how would I mock a querystring

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!

like image 950
MikeW Avatar asked Jul 31 '12 06:07

MikeW


People also ask

How do I know if a QueryString exists?

Request. QueryString. Count != 0 will simply tell you if there are no parameters at all.

How do I pass QueryString?

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.

What is the use of QueryString?

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.


1 Answers

You can mock indexers with the SetupGet method:

ContextMocks.Request.SetupGet(r => r["mymodel"]).Returns(myModel);
like image 104
nemesv Avatar answered Oct 20 '22 05:10

nemesv