Trying to wrap my head around this

4 posts, 0 answers
  1. Wade
    Wade avatar
    5 posts
    Member since:
    Feb 2010

    Posted 19 May 2010 Link to this post

    Okay, I am quite new at UnitTesting and the Mocking thing so bear with me an my ignorance.
    Since MVC is great for UnitTesting, I thought I would start to try to wrap my head around these concepts/practices for several reasons. 
    So...I was looking at MOQ and then I remembered Telerik had a beta going of JustMock, So I decided to try JustMock first.

    So, in delving into unit tests, mocking, and MVC, I came across the following link: which has some Mvc http mocking helpers.  Which I want to ultimately try to use to test a controller doing a post.  Since my controller also looks/processes some info from the HttpRequestBase Controller.Request, I think I need to mock the request in my unit test (could be wrong, but makes sense). Anyway, after reading the above link, it became painfully obvious that different mock libraries use different syntax, making things all that more confusing. The link uses examples for Rhino, Moq, and TypeMock..all slightly different.. grrr.  I learn by examples, trying, so I am trying to equate what I see in the above link to how it needs to be in JustMock.

    In their example (I'll just focus on the Moq, since that's my second choice for mocking and seems to be a good/acceptable mocking tool)
    public static void SetHttpMethodResult(this HttpRequestBase request, string httpMethod) 
        .Expect(req => req.HttpMethod) 
    is the following a correct JustMock equiv?
    public static void SetHttpMethodResult(this HttpRequestBase request, string httpMethod) 
        var test = Mock.Create<HttpRequestBase>(); 
        Mock.Arrange(() => test).Returns(request); 
        Mock.Arrange(() => test.HttpMethod).Returns(httpMethod); 

    and another:
    public static HttpContextBase FakeHttpContext() 
        var context = new Mock<httpcontextbase>(); 
        var request = new Mock<httprequestbase>(); 
        var response = new Mock<httpresponsebase>(); 
        var session = new Mock<httpsessionstatebase>(); 
        var server = new Mock<httpserverutilitybase>(); 
        context.Expect(ctx => ctx.Request).Returns(request.Object); 
                context.Expect(ctx => ctx.Response).Returns(response.Object); 
        context.Expect(ctx => ctx.Session).Returns(session.Object); 
        context.Expect(ctx => ctx.Server).Returns(server.Object); 
        return context.Object; 
    //my attempt to convert to JustMock: 
    public static HttpContextBase FakeHttpContext() 
        var context = Mock.Create<HttpContextBase>(); 
        var request = Mock.Create<HttpRequestBase>(); 
        var response = Mock.Create<HttpResponseBase>(); 
        var session = Mock.Create<HttpSessionStateBase>(); 
        var server = Mock.Create<HttpServerUtilityBase>(); 
        Mock.Arrange(() => context.Request).Returns(request); 
        Mock.Arrange(() => context.Response).Returns(response); 
        Mock.Arrange(() => context.Session).Returns(session); 
        Mock.Arrange(() => context.Server).Returns(server); 
        return context; 
    Right? Wrong?

    Thank you for your time.

  2. Ricky
    Ricky avatar
    467 posts

    Posted 20 May 2010 Link to this post

    Hello Wade,

    Thanks for the post. The following code snippet with moq :

    public static void SetHttpMethodResult(this HttpRequestBase request, string httpMethod)  
        .Expect(req => req.HttpMethod)  

    Equals  to :

    public static void SetHttpMethodResult(this HttpRequestBase request, string httpMethod)
        Mock.Arrange(() => request.HttpMethod).Returns(httpMethod);

    But your second pasted snippet is correct :-).

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  3. DevCraft R3 2016 release webinar banner
  4. Mihai
    Mihai avatar
    4 posts
    Member since:
    Aug 2010

    Posted 04 Mar 2011 Link to this post


    I am trying to properly instantiate an MVC controller so that I can test an action, but can't figure out how to do it. I peek ending up with null fields in the controller instance. Could you please paste a full working test (or link to one) for something like this?

  5. Ricky
    Ricky avatar
    467 posts

    Posted 09 Mar 2011 Link to this post

    Hi Mihai,

    Thanks again for making the post. A sample project that shows how to mock controller action is posted under the following thread:

    Please check it out and let us know if you are still having issues.

    Kind regards,
    the Telerik team
    Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
Back to Top