Solved

mock web context

Posted on 2015-01-06
5
257 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 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

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…
Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

770 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