7

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!

1 Answer 1

6

You can mock indexers with the SetupGet method:

ContextMocks.Request.SetupGet(r => r["mymodel"]).Returns(myModel);
Sign up to request clarification or add additional context in comments.

1 Comment

I added another related question here if you have time stackoverflow.com/questions/11751651/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.