Philippe Auriou
Philippe Auriou

Reputation: 577

Mocking HttpContext.Current.Application

I try to test Mock HttpContext.Current.Application third library which uses the following methods:  

HttpContext.Current.Application.Add ( "key","value");
HttpContext.Current.Application.Get ("key");

I tested Lots of mocking frameworks Moq, RhinoMock, FakeSystemWeb, FakeHttpContext But it is impossible to add value in the Application Dictionary, always HttpContext.Current.Application.Count == 0

The only solution that works is with Microsoft.Fakes, but alas it is only with the Premium and Ultimate versions, and developers to whom I provided the tests only the Professional Version !!

With Microsoft.Fakes (it works) :

 public MockHttpContext()
 {
      //MOCK System.Web
      _shimsContext = ShimsContext.Create();
      var httpRequest = new HttpRequest("", "http://www.monsite.com", "");
      var httpContext = new HttpContext(httpRequest, new(HttpResponse(new(StringWriter()));
      var applicationState = httpContext.Application;
      System.Web.Fakes.ShimHttpContext.CurrentGet = () => httpContext;
      System.Web.Fakes.ShimHttpContext.AllInstances.ApplicationGet = context => applicationState;
   }

Do you have an idea or how to distribute my test Microsoft.Fakes, or another framework of Mocking?

Thank you.

Upvotes: 1

Views: 4824

Answers (3)

JamesR
JamesR

Reputation: 745

Great framework for mocking HttpContext is Typemock Isolator. You can do it like in example below:

 [TestMethod, Isolated]
 public void TestMethod1()
 {
     var httpRequest = new HttpRequest("", "http://www.monsite.com", "");
     var httpContext = new HttpContext(httpRequest, new HttpResponse(new StringWriter()));
     var httpApp = httpContext.Application;

     Isolate.Fake.AllInstances<HttpContext>();

     Isolate.WhenCalled(() => HttpContext.Current).WillReturn(httpContext);
     Isolate.WhenCalled(() => HttpContext.Current.Application).WillReturn(httpApp);

     HttpContext.Current.Application.Add("key1", "value1");
     HttpContext.Current.Application.Add("key2", "value2");
     HttpContext.Current.Application.Add("key3", "value3");

     Assert.AreEqual(3, HttpContext.Current.Application.Count);
     Assert.AreEqual("value1", HttpContext.Current.Application.Get("key1"));
}

Upvotes: 1

urasandesu
urasandesu

Reputation: 1

Prig can that. You can write the code to mock HttpContext like the below:

public MockHttpContext()
{
    //MOCK System.Web
    _indirectionsContext = new IndirectionsContext();
    var httpRequest = new HttpRequest("", "http://www.monsite.com", "");
    var httpContext = new HttpContext(httpRequest, new HttpResponse(new StringWriter()));
    var applicationState = httpContext.Application;
    System.Web.Prig.PHttpContext.CurrentGet().Body = () => httpContext;
    System.Web.Prig.PHttpContext.ApplicationGet().Body = context => applicationState;
}

Upvotes: 0

CountZero
CountZero

Reputation: 6389

You should always use HttpContextBase, HttpRequestBase and HttpResponseBase in your application as apposed to the concrete versions which are impossible to test (without typemock, Microsoft.Fakes or some other magic).

Simply use the HttpContextWrapper class to convert as shown below.

var httpContextBase = new HttpContextWrapper(HttpContext.Current);

Upvotes: 0

Related Questions