1

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.

1

3 Answers 3

1

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")); } 
Sign up to request clarification or add additional context in comments.

2 Comments

Yes it looks very nice too, but it is not free to mock static variables :-(
Yep, it's sad( But it's worth it
0

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); 

5 Comments

The problem is that when we want to access static variables of HttpContext.Current.Application in the end we go through a singleton HttpContext.Current.ApplicationInstance and only Microsoft.Fake is able to handle this scenario. But alas this framework is exclusively on version Ultimate and Premium visual studio. If anyone has a solution that works would be coool. But I did a lot of research on the internet and everyone has the same problem.
contextWrapper.ApplicationInstance = new HttpApplication(); //var contextWrapper = MockRepository.GenerateStub<HttpContextWrapper>(HttpContext.Current); //HttpContext.Current = ((HttpContextBase) contextWrapper).ApplicationInstance.Context; //Don't possible HttpContext.Current.Application.Add("titi", "toto"); var length = HttpContext.Current.Application.Count; Check.That(length).IsGreaterThan(0); //Fail
I want fake static member >> System.Web.HttpContext.Current.Application of System.Web.dll !! because a third-party library uses the following syntax System.Web.HttpContext.Current.Application.Add ( "key", "value") System.Web.HttpContext.Current.Application.Get ( "key")
I don't have VS available right now but seem to remember coming across a similer issue where I deciced not to mock the object and simply use a conrete instance instead. If you are just testing the dictionary functionality would this be an option?
this is not possible as I can modify the code of third-party library. And access to definately System.Web.HttpContext.Current.Application is used in many places of this library.
0

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; } 

5 Comments

Great, by cons I have a problem to activate the extension in Visual Studio: Could not load type 'Urasandesu.NAnonym.Mixins.System.Security.Principal.WindowsIdentityMixin' from assembly 'Urasandesu.NAnonym, Version = 0.2.0.0, Culture = neutral, PublicKeyToken = ce9e95b04334d5fb'.
Oh! You have installed v1 family, haven't you? Probably, old assemblies still remains in GAC. Please execute the following commands in the Developer Command Prompt as Administrator: gacutil /u "Urasandesu.NAnonym, Version=0.2.0.0, Culture=neutral, PublicKeyToken=ce9e95b04334d5fb, processorArchitecture=MSIL" & gacutil /u "Urasandesu.Prig.Framework, Version=0.1.0.0, Culture=neutral, PublicKeyToken=acabb3ef0ebf69ce, processorArchitecture=MSIL"
I solved the problem, but it is not possible to setter values with Prig ! >> HttpContext.Current.Application.Add ( "key","value"); HttpContext.Current.Application.Get ("key");
I have uploaded the full sample to my google drive. Does this match to what you want?
Prig only works with Visual Studio 2013 and 2015 so this answer is now deprecated

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.