Solved

mock web context

Posted on 2015-01-06
5
307 Views
Last Modified: 2015-01-22
mock httpcontext or httpbasecontext...

second post.

I have spent many hours.  I know its possible, but I can google only fragments of what I want to do.

Ideally, I wand to use Moq in a unit test and mock some type of web context.

Please include complete example
0
Comment
Question by:kblau
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 20

Expert Comment

by:Daniel Van Der Werken
ID: 40535525
I ran into this problem last year. I cant' recall the exact way I resolved it, so I'll try to re-discover what I did and share it with you.

First off, you can't Mock HttpContext. You Mock HttpContextBase. I recall the HttpContextWrapper being used. Let's see where we go with that info:

1. Check out this web blog post describing the exact thing you want. It has examples.

2. Note that the Microsoft info regarding HttpContextBase says:
"The HttpContextBase class is an abstract class that contains the same members as the HttpContext class. The HttpContextBase class enables you to create derived classes that are like the HttpContext class, but that you can customize and that work outside the ASP.NET pipeline. When you perform unit testing, you typically use a derived class to implement members with customized behavior that fulfills the scenario you are testing.
The HttpContextWrapper class derives from the HttpContextBase class. The HttpContextWrapper class serves as a wrapper for the HttpContext class. At run time, you typically use an instance of the HttpContextWrapper class to call members on the HttpContext object."

3. More info regarding the HttpConextWrapper class that I previously mentioned.

If this information doesn't get you started where you need to go, let me know. I no longer work for the company I used to work for last year where I dealt with this issue, so I can't dig up my exact working example code. I know I didn't provide you with any exact samples, but this information should get you started.
0
 
LVL 5

Author Comment

by:kblau
ID: 40535862
at work just have time to see I already have a post.  Really appreciate it.  

Want to add that I am using Ninject

Thank you
0
 
LVL 5

Author Comment

by:kblau
ID: 40537315
Unit testing and mocking web context is not truly mocking the web context.  Which might be impossible.  But there are many posts that imply mocking of context, and I would like to put this dog to sleep.  

Someone posted that HttpContext (which is replaced by HttpContextBase) code printed out would circle the search 7 times (huge object).

Good post.  I got the unit test to work.  I will give you points if you can just answer this question:  

Is there a way to get MapPath to work in a unit test?  Is this called integration testing, which is really trully mocking web context?

Thanks.

Please, just off the top of your head, can you reply so I can let this dog sleep.  Any response with your thoughts and I will give you the points.  Much appreciated.
0
 
LVL 20

Accepted Solution

by:
Daniel Van Der Werken earned 500 total points
ID: 40547631
Sorry. I didn't see your request until just now.

Regarding MapPath: You could try this:
http://msdn.microsoft.com/en-us/library/system.web.hosting.hostingenvironment.mappath.aspx

Otherwise, try:
http://msdn.microsoft.com/en-us/library/system.appdomain.basedirectory%28v=vs.110%29.aspx

Sometimes people call integration testing unit testing. Ideally, a unit test will only look at one specific thing. Integration tests look at more than one. I'm not 100% sure what you're doing, but it's probably integration testing really.
0
 
LVL 5

Author Comment

by:kblau
ID: 40564582
accidently chose Dan7el's last answer, meant to select Dan's previous answer.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question