Solved

mock web context

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

Suggested Solutions

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

789 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