Solved

mock web context

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

Expert Comment

by:Daniel Van Der Werken
Comment Utility
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
Comment Utility
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
Comment Utility
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 19

Accepted Solution

by:
Daniel Van Der Werken earned 500 total points
Comment Utility
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
Comment Utility
accidently chose Dan7el's last answer, meant to select Dan's previous answer.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
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…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

771 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now