Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

mock web context

Posted on 2015-01-06
5
Medium Priority
?
348 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
  • 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 1500 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Screencast - Getting to Know the Pipeline

971 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