HttpContext Mocking

4 posts, 0 answers
  1. Ian
    Ian avatar
    4 posts
    Member since:
    Sep 2010

    Posted 11 Jan 2011 Link to this post

    I'm trying to test some web aware code using HttpContext.Current (Request & Server).

    JustMock is supposed to support this, but the working example is very simple and not what I want to achieve.

    What I Want to mock is

    if (HttpContext.Current != null)
    {
    string s = HttpContext.Current.Request....
    string t = HttpContext.Current.Server....
    }

    Can Justmock do this very simple task (I'm using the professional version)?.

    Thanks

    Ian
  2. Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 12 Jan 2011 Link to this post

    Hello Ian,
    Thanks for making the post. In regard to your case above, I  created a test method that returns custom request object from the mocked context and finally asserts it.

    Accordingly, I have a target method:

    private HttpRequest GetCurrentRequest()
    {
        if (HttpContext.Current != null)
        {
            return HttpContext.Current.Request;
        }
        return null;
    }


    Then wrote the test method in the following way:

    [TestMethod]
     public void TestMethod1()
     {
         var request = new HttpRequest("/", "http://telerik.com", "q=1");
         Mock.Arrange(() => HttpContext.Current.Request).Returns(request);
         Assert.AreEqual(GetCurrentRequest(), request);
         Mock.Assert(() => HttpContext.Current.Request);
    }

    Since, Request is a nested property it additionally illustrates the nested mocking capabilities of JustMock that can save you additional lines.

    Here to mention that to mock HttpContext and its members you need to add additional MockedClassAttribute on top your test class as like mocking mscorlib types.

    Finally, I attached the test project to also let you have a look and please do feel free to write back for any issues.


    Kind Regards,
    Ricky
    the Telerik team
    Browse the videos here>> to help you get started with JustMock
  3. DevCraft R3 2016 release webinar banner
  4. Ian
    Ian avatar
    4 posts
    Member since:
    Sep 2010

    Posted 12 Jan 2011 Link to this post

    Ricky,

    many thanks for the reply, especially the working example, which works, but it doesnt work in my context, its probably me being stupid..

    The class I'm testing has the following constructor:
    public OriginatingRequest()
    {
        if (HttpContext.Current != null)
        {

    My test code calls this (using your example)

     

    var request = new HttpRequest("/", "<A href="http://telerik.com">http://telerik.com<;/A>", "q=1");
      
    Mock.Arrange(() => HttpContext.Current.Request).Returns(request);
       
    OriginatingRequest target = new OriginatingRequest();

    The check for HttpContext.Current != null fails, I dont know why, I was having the same issue before, I thought that MsCorLib classes would be picked up by my test class.

    PS I am setting the MockClass attribute on my test class, do I need to do it on the target class too?

    Regards

    Ian


  5. Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 14 Jan 2011 Link to this post

    Hi Ian,
    Thanks again for reporting the issue. Actually, this is a nice catch and HttpContext.Current do failing when wrapped around a class. However, if I apply a MockedClassAttribute on the target, the test passes. In case of third-party classes this is not possible at any chance.

    Therefore, i am adding this as a bug which you can track at:
    http://www.telerik.com/support/pits.aspx#/public/justmock/4614

    Finally, sorry for the inconvenience and please do feel free to write us for any further issues.


    Kind Regards,
    Ricky
    the Telerik team
    Browse the videos here>> to help you get started with JustMock
Back to Top